aspose file tools*
The moose likes XML and Related Technologies and the fly likes Need Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need Help" Watch "Need Help" New topic
Author

Need Help

MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
I am new to XML.i am getting "java.lang.NullPointerException" when i run the program.pls give me advice.My java code is

import java.sql.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.IOException;
import java.util.StringTokenizer;

public class JAXP5
{
static Connection db=null;
static Statement statement=null;
static String thisusername=null;
static String thispassword=null;
static String thisurl=null;
static String thisdriver=null;
static Document d ;
static Element thisconn;


public static void main(String[] args)
{

String connectFile=args[0];

try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
//JAXP5 counter = new JAXP5();
try
{
d = parser.parse(connectFile);
}
catch(Exception e)
{
System.err.println(e);
}
try
{
Class.forName(thisdriver);
System.out.println("Connected");
db = DriverManager.getConnection(thisurl,thisusername,thispassword);
statement = db.createStatement();
System.out.println("DBConnected");
}
catch(Exception e)
{
System.err.println(e);
}
try
{
Element connectRoot = d.getDocumentElement();
NodeList dbconn = connectRoot.getElementsByTagName("dbconnection");

for (int i = 0; i < dbconn.getLength(); i++)
{
Element thisconn = (Element)dbconn.item(i);
}
}
catch(Exception e)
{
System.err.println(e);
}

thisusername = thisconn.getElementsByTagName("username")
.item(0)
.getFirstChild().getNodeValue();
System.out.println("Username: " + thisusername);

thispassword = thisconn.getElementsByTagName("password")
.item(0)
.getFirstChild().getNodeValue();
System.out.println("Password: " + thispassword);

thisurl = thisconn.getElementsByTagName("url")
.item(0)
.getFirstChild().getNodeValue();
System.out.println("Url: " + thisurl);

thisdriver = thisconn.getElementsByTagName("driver")
.item(0)
.getFirstChild().getNodeValue();
System.out.println("Driver: " + thisdriver);

}

catch(Exception e)
{
System.err.println(e);
}


}
}

and my XML code is

<dbconnection>
<username>jim</username>
<password>jim123</password>
<url>jdbc:mysql://192.168.1.49/jibin</url>
<driver>com.mysql.jdbc.Driver</driver>
</dbconnection>

hope i could get a reply soon.

minu


God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
It would help if you'd
1) surround your Java code with the UBB CODE tags, and
2) tell us from which line the NullPointerException is thrown.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Thanks for the advice.is my logic correct in the program ?. The reason i am asking because the values(username,passsword,url,driver) are not getting printed.hope you would debug the program.

i think the problem resides in this part of the code :

Element connectRoot = d.getDocumentElement();
NodeList dbconn = connectRoot.getElementsByTagName("dbconnection");
}
catch(Exception e)
{
System.out.println("Error creating class: "+e.getMessage());
}

for (int i = 0; i < dbconn.getLength(); i++)
{
Element thisconn = (Element)dbconn.item(i);
}


hope i could get a reply soon.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I would strongly recommend using "e.printStackTrace()" instead of "System.err.println(e)" because the former gives you essential information about where the exception originates from.

Anyway, your current problem is this line:
Class.forName(thisdriver);

You're getting a NullPointerException because 'thisdriver' is null when this statement is executed.
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Hi,
Thanks for the reply....

Yes....you are right.....

Can u plz suggest me a good solution...here in the code below, i am not getting 'username' printed.do you find any problem in my code and logic.

------------------------------------------------------------------------
thisusername = thisconn.getElementsByTagName("username").item(0)
.getFirstChild().getNodeValue();

System.out.println("Username: " + thisusername);


-------------------------------------------------------------------------

i am totally confused...
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I'd suggest that you split your code from the huge main() method into multiple, smaller methods like:

This way, you'll have a lot easier time fixing things one small piece at a time.
 
 
subject: Need Help