Results 1 to 20 of 24
Thread: Accessing list out another class
- 01-04-2008, 12:28 PM #1
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?
Java 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, 12:58 PM #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:
Java Code:class List{ private String[] data; public String[] getData(){ return data; } public void setData(String[] data){ this.data = data; } }
Java Code:String record = list.getData();
Last edited by tim; 01-04-2008 at 01:00 PM.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-04-2008, 01:35 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
List not the class defined by me its the arraylist
- 01-04-2008, 01:38 PM #4
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
Its an not user defined class..Its an arrylist
- 01-04-2008, 02:02 PM #5
"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:
Java Code:List data = nodes.list;
Did I answer your question? If not, please rephrase it. :DEyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-04-2008, 02:27 PM #6
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, 06:46 AM #7
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, 09:36 AM #8
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.
Java Code:public nodes() { list = new ArrayList(); }
Java Code:static{ list = new ArrayList(); }
Hope you understood..dont worry newbie, we got you covered.
- 01-05-2008, 11:15 AM #9
Thanks root. I wondered what the static block was for. :D
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-07-2008, 10:24 AM #10
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, 11:17 AM #11
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, 01:14 PM #12
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;
Java 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, 03:00 PM #13
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, 06:47 AM #14
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, 06:50 AM #15
Please post node class as well ..
dont worry newbie, we got you covered.
- 01-08-2008, 06:52 AM #16
oops !! it was the first code .. my badd ..
dont worry newbie, we got you covered.
- 01-08-2008, 07:33 AM #17
I have changed the class name and variable names .. i am kinda allergic to high tech name like temp and node :) ..
Java 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, 07:38 AM #18
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, 08:04 AM #19Java 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.. */ }
Java 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); } }
Java 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 #20
Similar Threads
-
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 02:04 PM -
Accessing inner class from outer class (an example)
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 10:03 AM -
An example of accessing outer class from inner class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 10:01 AM -
Inner class accessing outer class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 09:59 AM -
Accessing one class from another class through swing
By kbyrne in forum AWT / SwingReplies: 5Last Post: 01-03-2008, 08:54 AM
Bookmarks