Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-04-2008, 01:28 PM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
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(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-04-2008, 01:58 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.

Last edited by tim : 01-04-2008 at 02:00 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-04-2008, 02:35 PM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
List not the class defined by me its the arraylist
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-04-2008, 02:38 PM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
Its an not user defined class..Its an arrylist
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-04-2008, 03:02 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
"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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-04-2008, 03:27 PM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
Yes,i created a new inatance of nodes class..Still its not giving me any datas..the list is empty
Thank you...
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-05-2008, 07:46 AM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
I created the instance of the class which contains the list....still the list is empty..Can anyone help me please...
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-05-2008, 10:36 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-05-2008, 12:15 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Thanks root. I wondered what the static block was for.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-07-2008, 11:24 AM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
My problem was not when i'm i try to access the list out side the class...
Within the class its working fine...
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-07-2008, 12:17 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-07-2008, 02:14 PM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 01-07-2008, 04:00 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 01-08-2008, 07:47 AM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 01-08-2008, 07:50 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Please post node class as well ..
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 01-08-2008, 07:52 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
oops !! it was the first code .. my badd ..
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 01-08-2008, 08:33 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 01-08-2008, 08:38 AM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
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();
}


}
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 01-08-2008, 09:04 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 10-26-2008, 03:42 AM
FaTMollY's Avatar
Member
 
Join Date: Oct 2008
Location: Croatia
Posts: 1
FaTMollY is on a distinguished road
Send a message via ICQ to FaTMollY
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts