cancel
Showing results for 
Search instead for 
Did you mean: 

XML parser with Nullpointer Exception and URL Malformed Exception

Former Member
0 Kudos

Hi,

I am trying to call the following class( method add()) but it is giving Nullpointer Exception and URL Malformed Exception. Kindly give the solution. I am giving the program and the comments where i got the Exceptions.

Kidly see the following code. I am getting the exceptions at " doc = saxbuilder.build(fileName); ". Iwant send back the flag after executing this code successfully.

package shoppingcart;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.io.IOException;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

import org.apache.log4j.Logger;

import shoppingcart.dao.DAOUserTable;

import shoppingcart.vo.UserTableVo;

public class UserDB

{

/* public static void main(String[] args) throws JDOMException,IOException

{

} */

Connection con;

DAOUserTable utdao = new DAOUserTable();

UserTableVo pvo = new UserTableVo();

String fileName = "D:
user.xml";

SAXBuilder saxbuilder=new SAXBuilder();

Document doc;

public UserDB() throws IOException,JDOMException

{

<b> doc = saxbuilder.build(fileName);// here I got Nullpointer Exception and URL Malformed Exception

}</b> public boolean add(){

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (ClassNotFoundException e)

{

System.out.println(e.getMessage());

}

try {

con=DriverManager.getConnection("jdbc:odbc:shoppingcart","sa","sa");

} catch (SQLException e) {

System.out.println("Exception ......3 "+e.getMessage());

}

Element root=doc.getRootElement();

Element UId;

UId = root.getChild("UId");

pvo.setUId(UId.getTextTrim());

Element UName;

UName = root.getChild("UName");

pvo.setUName(UName.getTextTrim());

Element Password;

Password = root.getChild("Password");

pvo.setPassword(Password.getTextTrim());

Element Address;

Address = root.getChild("Address");

Element Street;

Street = Address.getChild("Street");

pvo.setStreet(Street.getTextTrim());

Element City;

City = Address.getChild("City");

pvo.setCity(City.getTextTrim());

Element State;

State = Address.getChild("State");

pvo.setState(State.getTextTrim());

Element Country;

Country = Address.getChild("Country");

pvo.setCountry(Country.getTextTrim());

Element Zip;

Zip = Address.getChild("Zip");

pvo.setZip(Zip.getTextTrim());

Element Phone;

Phone = root.getChild("Phone");

pvo.setPhone(Phone.getTextTrim());

Element Mobile;

Mobile = root.getChild("Mobile");

pvo.setMobile(Mobile.getTextTrim());

Element Fax;

Fax = root.getChild("Fax");

pvo.setFax(Fax.getTextTrim());

Element EmailID1;

EmailID1 = root.getChild("EmailID1");

pvo.setEmailID1(EmailID1.getTextTrim());

Element EmailID2;

EmailID2 = root.getChild("EmailID2");

pvo.setEmailID2(EmailID2.getTextTrim());

Element UserType;

UserType = root.getChild("UserType");

pvo.setUserType(UserType.getTextTrim());

boolean flag = false;

try {

flag = utdao.updateUser(con,pvo);

} catch (Exception e)

{

System.out.println("Exception ......4 "+e.getMessage());

}

return flag;

}

}

Thanks & Regards

Rama Krishna

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Ram try using this

String fileName = "file:///D:/user.xml";

Regards,

Piyush

ps: please mark for useful answers.

Former Member
0 Kudos

I just checked the API for SAX Builder and it needs File as parameter

Document build(java.io.File file)

so replace

String fileName = "file:///D:/user.xml";

with

File fileName = new File("D:/user.xml");

http://www.jdom.org/docs/apidocs/org/jdom/input/SAXBuilder.html#build(java.io.File)

But I guess, even this should work so check out

String fileName = "file:///D:/user.xml";

Regards,

Piyush

ps: please mark for useful answers.

Answers (1)

Answers (1)

Former Member
0 Kudos

Rama,

Just use the following form:


final URL url = new URL("file", "", -1, "D:\user.xml");
/*...*/
saxbuilder.build( url.toExternalForm() );

VS

Former Member
0 Kudos

Rama,

...or even more explicit one:


final File file = new File("D:\user.xml");
/*...*/
saxbuilder.build(( file.toURL().toExternalForm() );

VS