
01-04-2008, 11:28 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
Accessing list out another class
I have stored values in a List(public static )...I want to use those values inside another class
without extending that class How can i?
|
Code:
|
public class nodes
{--------
public static List list;
public nodes()
{
list = new ArrayList();
}
private void printData()
{
System.out.println("No of Employees '" + list.size() + "'.");
/** this is the iterator i want to cal in Paint class **/ it = list.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
public void addData(Parameters)
{
/** adding to the list from this method
}---------------------
}
public class Paint
{
Iterator it2;
List list2;
nodes obj;
----------
public void print()
{
/** trying to reterive node class's list here
list2 = obj.list;
it2 = list2.iterator();
}
} |
|
|

01-04-2008, 11:58 AM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 337
Rep Power: 2
|
|
Accessors and Mutators
Hello Preethi.
I assume that List is a class defined by you. In the definition of your list class add a accessor and mutator (get and set methods) to you data structure. For example:
|
Code:
|
class List{
private String[] data;
public String[] getData(){
return data;
}
public void setData(String[] data){
this.data = data;
}
} |
Now you should be able to access the data by using the dot notation:
|
Code:
|
String record = list.getData(); |
Hope this helped.
__________________
Grudges are heavy. Why hold them when you can let them go?
Last edited by tim; 01-04-2008 at 12:00 PM.
|
|

01-04-2008, 12:35 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
|
List not the class defined by me its the arraylist
|
|

01-04-2008, 12:38 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
|
Its an not user defined class..Its an arrylist
|
|

01-04-2008, 01:02 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 337
Rep Power: 2
|
|
"public static"
Sorry, I just spotted the public static modifier now. You should be able to access your list anywhere in your package like this:
|
Code:
|
List data = nodes.list; |
You do not have to create an new instance nodes or extend that class. Just be careful for reading a static field with a null reference.
Did I answer your question? If not, please rephrase it.
__________________
Grudges are heavy. Why hold them when you can let them go?
|
|

01-04-2008, 01:27 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
|
Yes,i created a new inatance of nodes class..Still its not giving me any datas..the list is empty
Thank you...
|
|

01-05-2008, 05:46 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
|
I created the instance of the class which contains the list....still the list is empty..Can anyone help me please...
|
|

01-05-2008, 08:36 AM
|
 |
Moderator
|
|
Join Date: Jan 2008
Location: Dallas
Posts: 289
Rep Power: 2
|
|
Ok even though your list is static but it is not statically initiated. It is always null unless you do make instance of node using new operator.
|
Code:
|
public nodes()
{
list = new ArrayList();
} |
Replace above code with this
|
Code:
|
static{
list = new ArrayList();
} |
This will initiate your list statically and it wont be empty ..
Hope you understood..
__________________
dont worry newbie, we got you covered.
|
|

01-05-2008, 10:15 AM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 337
Rep Power: 2
|
|
Thanks root. I wondered what the static block was for.
__________________
Grudges are heavy. Why hold them when you can let them go?
|
|

01-07-2008, 09:24 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
|
My problem was not when i'm i try to access the list out side the class...
Within the class its working fine...
|
|

01-07-2008, 10:17 AM
|
 |
Moderator
|
|
Join Date: Jan 2008
Location: Dallas
Posts: 289
Rep Power: 2
|
|
|
Preethi have you added the static block there ... ? When you access list from external class the list is not initiated ..
__________________
dont worry newbie, we got you covered.
|
|

01-07-2008, 12:14 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
Yes,even then its not initiated...
This is my class where i add to the list;
|
Code:
|
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class NewClass extends DefaultHandler
{
public static List list;
private String tempVal;
//Creating a reference for Class nodes
private nodes tempnode;
public static Iterator it;
public NewClass()
{
list = new ArrayList();
it = list.iterator();
}
public void runExample()
{
parseDocument();
printData();
}
private void parseDocument()
{
/** Reads the file**/
}
public static List printData()
{
System.out.println("No of Employees '" + list.size() + "'.");
it = list.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
return list;
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
//reset
tempVal = "";
if(qName.equalsIgnoreCase("res_mesg"))
{
//create a new instance of employee
tempnode = new nodes();
for(int len=0;len<attributes.getLength();len++)
{
tempnode.setcategory(attributes.getType("category"));
}
}
}
public void characters(char[] ch, int start, int length) throws SAXException
{
tempVal = new String(ch,start,length);
}
public void end(String uri, String localName, String qName)
{
if(qName.equalsIgnoreCase("res_mesg"))
{
//add it to the list
list.add(tempnode);
}
else if (qName.equalsIgnoreCase("res_no"))
{
tempnode.setresno(tempVal);
}
}
public static void main(String[] args)
{
NewClass spe = new NewClass();
spe.runExample();
}
} |
|
|

01-07-2008, 02:00 PM
|
 |
Member
|
|
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
Rep Power: 0
|
|
|
It looks like you are trying to run a SAX parser hence the extension of DefaultHandler? What exactly does:
private void parseDocument()
{
/** Reads the file**/
}
do? Are you not showing code here or is that it as written ( if so then your ArrayList will never have data in it. I would have expected to see a SAXParser being set up and handed an instance of your handler. The way SAX works is that it parses the XML document for you and calls back into your handler.
In passing be careful of your code in the characters method, SAX does _not_ guarantee that it will only call you once for each tags data, it may make several calls passing you part of the data each time so you should alter your code to _append_ the info as shown below:
tempVal += new String(ch,start,length);
|
|

01-08-2008, 05:47 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class NewClass extends DefaultHandler
{
public static List list;
private String tempVal;
//Creating a reference for Class nodes
private nodes tempnode;
public static Iterator it;
public NewClass()
{
list = new ArrayList();
it = list.iterator();
}
public void runExample()
{
parseDocument();
printData();
}
private void parseDocument()
{
/** Reads the file**/
}
public static List printData()
{
System.out.println("No of Employees '" + list.size() + "'.");
it = list.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
return list;
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
//reset
tempVal = "";
if(qName.equalsIgnoreCase("res_mesg"))
{
//create a new instance of employee
tempnode = new nodes();
for(int len=0;len<attributes.getLength();len++)
{
tempnode.setcategory(attributes.getType("category" ));
}
}
}
public void characters(char[] ch, int start, int length) throws SAXException
{
tempVal = new String(ch,start,length);
}
public void end(String uri, String localName, String qName)
{
if(qName.equalsIgnoreCase("res_mesg"))
{
//add it to the list
list.add(tempnode);
}
else if (qName.equalsIgnoreCase("res_no"))
{
tempnode.setresno(tempVal);
}
}
public static void main(String[] args)
{
NewClass spe = new NewClass();
spe.runExample();
}
}
This is the complete coding...I want to get the datas from this class to another..this is were i'm struck up.can anyone able to help me,please...
|
|

01-08-2008, 05:50 AM
|
 |
Moderator
|
|
Join Date: Jan 2008
Location: Dallas
Posts: 289
Rep Power: 2
|
|
|
Please post node class as well ..
__________________
dont worry newbie, we got you covered.
|
|

01-08-2008, 05:52 AM
|
 |
Moderator
|
|
Join Date: Jan 2008
Location: Dallas
Posts: 289
Rep Power: 2
|
|
|
oops !! it was the first code .. my badd ..
__________________
dont worry newbie, we got you covered.
|
|

01-08-2008, 06:33 AM
|
 |
Moderator
|
|
Join Date: Jan 2008
Location: Dallas
Posts: 289
Rep Power: 2
|
|
I have changed the class name and variable names .. i am kinda allergic to high tech name like temp and node  ..
|
Code:
|
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class EmployeeXMLHandler extends DefaultHandler {
private List<EmployeeNode> employees = new ArrayList<EmployeeNode>();
private EmployeeNode currentEmployeeNode; // Used in XML Processing only
public EmployeeXMLHandler() {}
private String characterChunk = "" ;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// We can not be sure that all character will be passed on single call ..
characterChunk = characterChunk + new String(ch,start,length);
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
characterChunk = "";
if (name.equalsIgnoreCase("res_mesg")) {
// create a new instance of employee
currentEmployeeNode = new EmployeeNode();
for (int len = 0; len < attributes.getLength(); len++) {
currentEmployeeNode.setcategory(attributes.getType("category"));
}
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
if (name.equalsIgnoreCase("res_mesg")) {
employees.add(currentEmployeeNode);
} else if (name.equalsIgnoreCase("res_no")) {
currentEmployeeNode.setresno(characterChunk);
}
}
public void print(){
Iterator<EmployeeNode> iterator = employees.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
public List<EmployeeNode> getEmployees(){
return this.employees;
}
public void process()throws Exception{
/*
* Load XML File and parse it using this as handler..
*/
}
public static void main(String[] args) throws Exception {
new EmployeeXMLHandler().process();
}
/**
* Same thing here .. why public static list .. ?
* If you want it to be public static you can change it..
*/
}
//--------------------
import java.util.Dictionary;
import java.util.Hashtable;
public class EmployeeNode {
private Dictionary<String, String> attributes = new Hashtable<String, String>();
public void setresno(String value) {
attributes.put("res_no", value);
}
public void setcategory(String value) {
attributes.put("category", value);
}
/**
* Please add accessor methods or make attributes public. I still cant understand why you need static thing here ?
* ...
*/
} |
__________________
dont worry newbie, we got you covered.
|
|

01-08-2008, 06:38 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 83
Rep Power: 0
|
|
|
package MyApplet;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class nodes
{
String resno;
private String String;
NewClass xml;
static List<xml> list1;
Iterator it ;
java.lang.String category;
public nodes()
{
list1 = new ArrayList<xml>();
}
public nodes(String resno,String category)
{
this.category = category;
this.resno = resno;
}
public String getresno()
{
return resno;
}
public String getcategory()
{
return category;
}
public void setresno(String resno)
{
this.resno = resno;
}
public void setcategory(String category)
{
this.category = category;
}
public String toString()
{
StringBuffer vt = new StringBuffer();
vt.append(getresno());
vt.append(getcategory());
return vt.toString();
}
}
|
|

01-08-2008, 07:04 AM
|
 |
Moderator
|
|
Join Date: Jan 2008
Location: Dallas
Posts: 289
Rep Power: 2
|
|
|
Code:
|
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class EmployeeXMLHandler extends DefaultHandler {
private List<EmployeeNode> employees = new ArrayList<EmployeeNode>();
private EmployeeNode currentEmployeeNode; // Used in XML Processing only
public EmployeeXMLHandler() {}
private String characterChunk = "" ;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// We can not be sure that all character will be passed on single call ..
characterChunk = characterChunk + new String(ch,start,length);
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
characterChunk = "";
if (name.equalsIgnoreCase("res_mesg")) {
// create a new instance of employee
currentEmployeeNode = new EmployeeNode();
for (int len = 0; len < attributes.getLength(); len++) {
currentEmployeeNode.setCategory(attributes.getValue("category"));
}
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
if (name.equalsIgnoreCase("res_mesg")) {
employees.add(currentEmployeeNode);
} else if (name.equalsIgnoreCase("res_no")) {
currentEmployeeNode.addResNo(characterChunk);
}
}
public void print(){
Iterator<EmployeeNode> iterator = employees.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
public List<EmployeeNode> getEmployees(){
return this.employees;
}
public void process()throws Exception{
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(this);
reader.parse(new InputSource(new FileInputStream("data.xml")));
print();
}
public static void main(String[] args) throws Exception {
new EmployeeXMLHandler().process();
}
/**
* Same thing here .. why public static list .. ?
* If you want it to be public static you can change it..
*/
} |
|
Code:
|
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class EmployeeNode {
private List<String> resNo = new ArrayList<String>();
private String category;
public String toString() {
String toStr = "Category = " + category + " & res no = ";
Iterator<String> iterator = resNo.iterator();
while (iterator.hasNext()) {
toStr = toStr + iterator.next() + ",";
}
return toStr.substring(0, toStr.length() - 1); // removing last coma
}
public List<String> getResNo() {
return resNo;
}
public void setResNo(List<String> resNo) {
this.resNo = resNo;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public void addResNo(String data) {
this.resNo.add(data);
}
} |
Output
|
Code:
|
Category = 1 & res no = 101,102
Category = 2 & res no = 201,202 |
__________________
dont worry newbie, we got you covered.
|
|

10-26-2008, 02:42 AM
|
 |
Member
|
|
Join Date: Oct 2008
Location: Croatia
Posts: 1
Rep Power: 0
|
|
help me please! windows 98 - dont know...
At me such question
I can not change the menu in Windows, it looks on new why that....
Help to adjust... At me of a Window 98
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 05:44 PM.
|
|