Results 1 to 9 of 9
  1. #1
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default NullPointer Exception

    I tried to add the datas to the list and trying to access in another class but its not working..throwing me null pointer exception...

    Getter and setter method class
    Java Code:
    package saxExamples;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    
    public class DataReader 
    {
        String category;
       
        XMLReading xmlreadingObj;
        
        private List<String> res_mseg = new ArrayList<String>();
        /** Creates a new instance of DataReader */
       
        /*Setter method for the attribute category in tag <res_mseg>*/
        public void setCategory(String category)
        {
            this.category = category;
           
        }
        
        /* Getter method for the List */
        public void setres_mseg(List<String> res_mseg)
        {
            this.res_mseg = res_mseg;
        }
        
        public void addres_mseg(String data)
        {
            this.res_mseg.add(data);
        }
        /*Getter method for the attribute category in tag <res_mseg>*/
        
        public String getCategory()
        {
            return category;
        }
       
        
        /* Getter method for the List */
        
        public List getres_mseg()
        {
            return res_mseg;
        }
        
        public void getData()throws Exception
        {
            /*Instantiating Obj of  
             *Class XMLReading
             */
             
            xmlreadingObj = new XMLReading();
            xmlreadingObj.fileParsing();
            
            /*Assigning the list variable to  the 
             *List in the class XMLReading 
             */
            List<DataReader> list = xmlreadingObj.getres_list();
            Iterator<DataReader> it = list.iterator();
            
            while(it.hasNext())
            {
                /*Iterating thro' the DataReader
                 *Class Object
                 */
                DataReader obj =  it.next();
                List res = obj.getres_mseg();
                String category = obj.getCategory(); 
                
                
                System.out.println(it.next());
           }
        }
        
        public static void main(String args[])
        {
            DataReader Obj = new DataReader();
            try 
            {
                Obj.getData();
            }
            catch (Exception ex) 
            {
                ex.printStackTrace();
            }
        }
        
        
    }
    Data was read and stored in this class
    Java Code:
    package saxExamples;
    
    
    import com.sun.org.apache.xerces.internal.parsers.XMLParser;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javax.print.attribute.AttributeSet;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.XMLReaderFactory;
    import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.XMLReader;
    import org.xml.sax.InputSource;
    import java.io.FileReader;
    import org.xml.sax.Attributes;
    
    public class XMLReading extends DefaultHandler
    {
    	List res_list ;
    	DataReader datareaderObj;
    	boolean isres_mseg = false;
    	/** Creates a new instance of XmlParser */
    
    	public void fileParsing() throws SAXException, FileNotFoundException, IOException
    	{
    		XMLReader reader = XMLReaderFactory.createXMLReader();
    		reader.setContentHandler(this);
    		reader.parse(new InputSource(new FileReader("C:/Documents and Settings/Gantt.xml")));
    	}
    
    
    	public void startElement (String uri, String localName, String qName, Attributes attri) 
    	{    
    		if (qName.equals("res_mseg")
    		{        
    			isres_mseg = true;   
    		} 
    
    		if (isres_mseg && qName.equals("I")) 
    		{        
    			String category = attri.getValue("category");        
    			String resno = attri.getValue("resno");        
    			datareaderObj.setCategory(attri.getValue("categort"));       
    			res_list.add(datareaderObj);
    		}
    	}
    
    	public void endElement (String name,String localName,String qName) 
    	{   
    		if (qName.equals("res_mseg"))    
    		{
    			res_list.add(datareaderObj);
    		}
    
    		isres_mseg = false;
    	}
    	
    	public List<DataReader> getres_list()
    	{
    		return  res_list;
    	}
    
    	public void print()
    	{
    		Iterator it = res_list.iterator();
    		while(it.hasNext())
    		{
    			System.out.println(it.next());
    		}
    	}
    
    	public static void main(String args[]) throws SAXException, FileNotFoundException, IOException
    	{
    		XMLReading  obj = new XMLReading();
    		obj.fileParsing();
    		obj.print();
    	}
    
    }

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

    Default

    any help..
    pls

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.util.List;
    
    public class DataReaderTest
    {    
        public void getData() throws Exception
        {
            DataReaderRx reader = new DataReaderRx();
            reader.readData("readData.txt");
            
            List<String> list = reader.getData();
            Iterator<String> it = list.iterator();
            
            while(it.hasNext())
            {
                /*Iterating thro' the List
                 */
                String line =  (String)it.next();            
                System.out.println(line);
           }
        }
        
        public static void main(String args[])
        {
            DataReaderTest obj = new DataReaderTest();
            try 
            {
                obj.getData();
            }
            catch (Exception ex) 
            {
                ex.printStackTrace();
            }
        }
    }
    
    class DataReaderRx
    {
        List<String> dataList = new ArrayList<String>();
    
        public void readData(String path) throws IOException {
            BufferedReader br = new BufferedReader(
                                new InputStreamReader(
                                new FileInputStream(new File(path))));
            String line;
            while((line = br.readLine()) != null)
            {
                dataList.add(line);
            }
            br.close();
        }
        
        /* Getter method for the List */
        public List<String> getData()
        {
            return this.dataList;
        }
    }
    readData.txt
    Java Code:
    Reads text from a 
    character-input stream, 
    buffering characters so 
    as to provide for the 
    efficient reading of 
    characters, arrays, and lines.

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

    Default

    I'm reading an xml file and getting the datas as java obj...and i'm trying to put it an arraylist and reterive in another class..
    Here where i'm getting NullPointer Exception

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

    Default

    can anyone help me,pls

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

    Default Attachment

    Hello Preethi
    Quote Originally Posted by Preethi
    Java Code:
    reader.parse(new InputSource(new FileReader("C:/Documents and Settings/Gantt.xml")));
    Can you please post an attachment of the xml document, so that I can work with the file.

    Thank you. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

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

    Default

    this is my xml
    <RC-BOD>
    <!--The datas in this tag is not necessary-->
    <fw_context RecordCount="1">
    <I component="sch" service="schscht_ser_sav"

    componentinstance="0" sectoken="DEMOUSER:demorole:1"

    ouinstance="2" language="1" user="DEMOUSER"

    txnid="3E89B3EC7E434AD1A913B17B38E7FC9A"

    transactionobject="0" txntime="11/9/2006 11:10:13 AM"

    txnoutcome="105" />
    </fw_context>
    <!--The datas in this tag is not necessary-->
    <hdrseg RecordCount="1">
    <I dep="" fromresno="" fromtimeperiod="2007-11-01"

    guid="CA327CF4-7C5A-4017-888B-F3E556465154"

    hdncompanycode="COM001BU1" item_code1=""

    production_cont="WI" save_template="1" toresno=""

    totimeperiod="2008-04-30" wo_no="" />
    </hdrseg>
    <!-- need only resno and category-->
    <res_mseg>
    <I resno="101" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="102" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="103" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="104" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="105" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="106" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="201" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="202" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="203" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="204" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="205" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    <I resno="206" shortdesc="assembly" category="1"

    capcheck="D" rescapacity="1.00000000" controlunit2="PWIL" />
    </res_mseg>
    </RC-BOD>

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

    Default "Null pointer exception" gone ;)

    Hello Preethi

    I fixed the "null pointer exception" problem. I did not quite understand how you structured your program, so I tried my best. Mostly, your program had syntax errors and typos. But, more importantly, I added a DataObject class and changed DataReader to a usage class. So now we have:

    DataObject.java
    Java Code:
    /*
     * DataObject.java
     *
     */
    
    package saxexamples;
    
    public class DataObject {
        protected String category = "";
        protected String resno = "";
    
        public String getCategory() {
            return category;
        }
    
        public String getResno() {
            return resno;
        }
    
        public void setCategory(String category) {
            this.category = category;
        }
    
        public void setResno(String resno) {
            this.resno = resno;
        }
        
        public DataObject() {}
        
        public DataObject(String category, String resno) {
            this.category = category;
            this.resno = resno;
        }
        
        public String toString(){
            return "category: " + this.category + ", resno: " + this.resno;
        }
    }
    DataReader.java
    Java Code:
    package saxexamples;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class DataReader 
    {
        protected List<DataObject> res_mseg;
       
        public DataReader(){
            res_mseg = new ArrayList<DataObject>();
        }
        
        public void getData()throws Exception
        {
            XMLReading xmlreadingObj = new XMLReading();
            xmlreadingObj.fileParsing("data.xml");
            xmlreadingObj.print();
        }
        
        public static void main(String args[])
        {
            DataReader Obj = new DataReader();
            try 
            {
                Obj.getData();
            }
            catch (Exception ex) 
            {
                ex.printStackTrace();
            }
        }  
    }
    XMLReading.java
    Java Code:
    package saxexamples;
    
    import com.sun.org.apache.xerces.internal.parsers.XMLParser;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javax.print.attribute.AttributeSet;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.XMLReaderFactory;
    import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.XMLReader;
    import org.xml.sax.InputSource;
    import java.io.FileReader;
    import org.xml.sax.Attributes;
    
    public class XMLReading extends DefaultHandler
    {
        protected ArrayList<DataObject> res_list;
            
        protected boolean isres_mseg;
            
        public XMLReading(){
            res_list = new ArrayList<DataObject>();
            isres_mseg = false;
        }
        
        public void fileParsing(String filename) throws SAXException, FileNotFoundException, IOException
        {
            XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setContentHandler(this);
            reader.parse(new InputSource(new FileReader(filename)));
        }
    
        public void startElement (String uri, String localName, String qName, Attributes attri){    
        	if (qName.equals("res_mseg")){        
                isres_mseg = true;   
            } 
    
            if (isres_mseg && qName.equals("I")) {        
                String category = attri.getValue("category");        
                String resno = attri.getValue("resno");        
                res_list.add(new DataObject(category, resno));
            }
        }
    
        public void endElement (String name,String localName,String qName) 
        {   
            if (qName.equals("res_mseg")){
                isres_mseg = false;
            }
        }
    	
        public ArrayList<DataObject> getres_list(){
            return res_list;
        }
    
        public void print(){
            Iterator it = res_list.iterator();
            while(it.hasNext()){
                System.out.println(it.next().toString());
            }
        }
    }
    Note that there is only one main() method and that I used ArrayList objects to save the data. This is because a List cannot be instantiated explicitly. I see that hardwired did the same. Now, the reason you got the Null pointer exception, is that you did not define proper constructors for your classes. It is necessary to create an instance of a data structure before you try to add objects to it. If you run my code you will get the following
    output
    Java Code:
    category: 1, resno: 101
    category: 1, resno: 102
    category: 1, resno: 103
    category: 1, resno: 104
    category: 1, resno: 105
    category: 1, resno: 106
    category: 1, resno: 201
    category: 1, resno: 202
    category: 1, resno: 203
    category: 1, resno: 204
    category: 1, resno: 205
    category: 1, resno: 206
    I hope this helps you. Please try to learn from our method of programming. There are structures that you must plan and then implement.

    Good luck Preethi! :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

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

    Default Accessors

    Hello Preethi

    To get the information out of the list, you must use the accessors of the DataObject class. For example, let's say you have a class that can display some attributes of the items in the ArrayList:
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class DataReader 
    {
        protected List<DataObject> res_mseg;
       
        public DataReader(){
            res_mseg = new ArrayList<DataObject>();
        }
        
        public void getData()throws Exception
        {
            XMLReading xmlreadingObj = new XMLReading();
            xmlreadingObj.fileParsing("data.xml");
    [COLOR="RoyalBlue"]	Saved savedData = new Saved(xmlreadingObj.getres_list());
    	savedData.printCategories();[/COLOR]
        }
        
        public static void main(String args[])
        {
            DataReader Obj = new DataReader();
            try 
            {
                Obj.getData();
            }
            catch (Exception ex) 
            {
                ex.printStackTrace();
            }
        }
    }
    [COLOR="RoyalBlue"]class Saved{
    	[B]protected ArrayList<DataObject> data;[/B]
    	public Saved(ArrayList<DataObject> data){
    		this.data = data;
    	}
    	public void printCategories(){
    		for (DataObject dataObject : this.data){
    			System.out.println(dataObject.getCategory());
    		}
    	}
    }
    [/COLOR]
    To access some data from another class, you must reference the data structure when you create that class. The protected attribute serves as a pointer to the original data. The DataObject class has accessor methods (getters) that you can use to get the information. And about this:
    Quote Originally Posted by Preethi
    I want to read some other attribute from another tag(<rev_mseg>)..
    But each attribute in seperate ArrayList..So for each variable should i have to create one class?(cos, there should be only one toString() method in a class)I don't how to use the toString method..
    You can have multiple data structures for different attributes, but I recommend that you only have one that can hold some abstract class and then you can derive different attributes from that and then add it to the data structure. This will shorten your code greatly. ;)

    The toString() method is used to display data from objects easily. If you define a class then the toString() method of your new class will display the default information: Class name, Memory address, etc. This method can be overridden to return a String object with different information, like I did in the DataObject class:
    Java Code:
    public String toString(){
        return "category: " + this.category + ", resno: " + this.resno;
    }
    To override a method, you define the method heading to be exactly the same as that of the parent method and you add your code in the definition. This will block the parent methods behavior and do some other task.

    Please ask your questions on the forum, as I cannot always help.

    Good luck.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Need help on Exception
    By Deon in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 06:46 PM
  2. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 03:24 PM
  3. exception
    By Oktam in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 08:01 PM
  4. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 12:50 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
  •