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

02-05-2008, 08:46 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 78
|
|
|
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
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
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, 09:03 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 78
|
|
|
any help..
pls
|
|

02-05-2008, 09:39 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,144
|
|
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
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, 10:39 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 78
|
|
|
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, 12:42 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 78
|
|
|
can anyone help me,pls
|
|

02-05-2008, 01:13 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
Attachment
Hello Preethi
Originally Posted by Preethi
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. 
__________________
If your ship has not come in yet then build a lighthouse.
|
|

02-05-2008, 02:01 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 78
|
|
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, 05:21 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
"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
/*
* 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
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
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
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! 
__________________
If your ship has not come in yet then build a lighthouse.
|
|

02-06-2008, 04:40 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
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:
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");
Saved savedData = new Saved(xmlreadingObj.getres_list());
savedData.printCategories();
}
public static void main(String args[])
{
DataReader Obj = new DataReader();
try
{
Obj.getData();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
class Saved{
protected ArrayList<DataObject> data;
public Saved(ArrayList<DataObject> data){
this.data = data;
}
public void printCategories(){
for (DataObject dataObject : this.data){
System.out.println(dataObject.getCategory());
}
}
}
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:
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:
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.
__________________
If your ship has not come in yet then build a lighthouse.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|