Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Unhappy 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();
       
    } 
    
    }

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default 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;
        }
    }
    Now you should be able to access the data by using the dot notation:
    Java Code:
    String record = list.getData();
    Hope this helped. ;)
    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.

  3. #3
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    List not the class defined by me its the arraylist

  4. #4
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    Its an not user defined class..Its an arrylist

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default "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;
    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. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  6. #6
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    Yes,i created a new inatance of nodes class..Still its not giving me any datas..the list is empty
    Thank you...

  7. #7
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    I created the instance of the class which contains the list....still the list is empty..Can anyone help me please...

  8. #8
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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();
    }
    Replace above code with this

    Java 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.

  9. #9
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    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.

  10. #10
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    My problem was not when i'm i try to access the list out side the class...
    Within the class its working fine...

  11. #11
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.

  12. #12
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    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();
           
        }
       
    }

  13. #13
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    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);

  14. #14
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    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...

  15. #15
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Please post node class as well ..
    dont worry newbie, we got you covered.

  16. #16
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    oops !! it was the first code .. my badd ..
    dont worry newbie, we got you covered.

  17. #17
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.

  18. #18
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    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();
    }


    }

  19. #19
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java 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);
    	}
    
    }
    Output
    Java Code:
    Category = 1 & res no = 101,102
    Category = 2 & res no = 201,202
    dont worry newbie, we got you covered.

  20. #20
    FaTMollY's Avatar
    FaTMollY is offline Member
    Join Date
    Oct 2008
    Location
    Croatia
    Posts
    1
    Rep Power
    0

    Default 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

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  2. Replies: 0
    Last Post: 02-17-2008, 10:03 AM
  3. Replies: 0
    Last Post: 02-17-2008, 10:01 AM
  4. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 09:59 AM
  5. Replies: 5
    Last Post: 01-03-2008, 08:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •