Results 1 to 9 of 9
Thread: NullPointer Exception
- 02-05-2008, 07:46 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
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
Data was read and stored in this classJava 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(); } } }
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(); } }
- 02-05-2008, 08:03 AM #2
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
any help..
pls
- 02-05-2008, 08:39 AM #3
readData.txtJava 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; } }
Java Code:Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
- 02-05-2008, 09:39 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
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
- 02-05-2008, 11:42 AM #5
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
can anyone help me,pls
- 02-05-2008, 12:13 PM #6
Attachment
Hello Preethi
Can you please post an attachment of the xml document, so that I can work with the file.
Originally Posted by Preethi
Thank you. ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 02-05-2008, 01:01 PM #7
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
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>
- 02-05-2008, 04:21 PM #8
"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
DataReader.javaJava 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; } }
XMLReading.javaJava 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(); } } }
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 followingJava 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()); } } }
output
I hope this helps you. Please try to learn from our method of programming. There are structures that you must plan and then implement.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
Good luck Preethi! :DEyes dwelling into the past are blind to what lies in the future. Step carefully.
- 02-06-2008, 03:40 PM #9
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:
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: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]
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. ;)
Originally Posted by Preethi
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:
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.Java Code:public String toString(){ return "category: " + this.category + ", resno: " + this.resno; }
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
-
Need help on Exception
By Deon in forum New To JavaReplies: 7Last Post: 02-11-2010, 05:46 PM -
Where does the exception go?
By aytidaalkuhs in forum New To JavaReplies: 3Last Post: 04-07-2008, 02:24 PM -
exception
By Oktam in forum New To JavaReplies: 2Last Post: 03-23-2008, 07:01 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM -
Exception
By Camden in forum New To JavaReplies: 2Last Post: 11-26-2007, 11:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks