• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help

 
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic