CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2006
    Posts
    29

    [RESOLVED] NullPointerException at doGet line 63

    Greetings Java people!
    In my effort to understand the process of dealing with databases and web pages I am trying to execute a simple class that hopefully will display the contents back to a JSP page. However, I am getting an “java.lang.NullPointerExeception”
    At SQL.SQLGatewayServlet.doGet(SQLGatewayServlet.java:63)

    Code:
    package SQL;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import util.*;
    
    public class SQLGatewayServlet extends HttpServlet
    {
    	private Connection connection;
    	private String file;
    	private String dbDriver;
    	private String dbURL;
    	private String dbUser;
    	private String dbPassword;
    
    	public void init() throws ServletException
    	{
    		ServletConfig config = getServletConfig();
    		file = config.getInitParameter("fileName");
    		dbDriver = config.getInitParameter("jdbcDriver");
    		dbURL = config.getInitParameter("dbUrl");
    		dbUser = config.getInitParameter("username");
    		dbPassword = config.getInitParameter("password");
    		
    		try
    		{
    			Class.forName(dbDriver);
    			connection = DriverManager.getConnection(dbURL,dbUser,dbPassword);
    		}
    		catch (ClassNotFoundException e)
    		{
    			System.out.println("Database Driver Not Found ");
    		}
    		catch (SQLException e)
    		{
    			System.out.println("Error Opening the DB connection: " + e.getMessage());
    		}
    	}
    
    	public void destroy()
    	{
    		try
    		{
    			connection.close();
    		}
    		catch (SQLException e)
    		{
    			System.out.println("Error closing the db connection: " + e.getMessage());
    		}
    	}
    
    	public void doGet(HttpServletRequest request,
    					  HttpServletResponse response)
    					  throws IOException, ServletException
    	{
    		String sqlStatement = request.getParameter("sqlStatement");
    		String message = "";
    
    		try
    		{			
    			Statement statement = connection.createStatement();
    			sqlStatement = sqlStatement.trim();
    			String sqlType = sqlStatement.substring(0,6);
    
    			if(sqlType.equalsIgnoreCase("select"))
    			{
    				ResultSet resultSet = statement.executeQuery(sqlStatement);
    				message = SQLUtil.getHtmlRows(resultSet);
    			}
    			else
    			{
    				int i = statement.executeUpdate(sqlStatement);
    				if(i == 0)
    					message = "The statement executed successfully.";
    				else
    					message = "The statement executed successfully.<br/>" + i + "row(s) affected.";
    			}
    			statement.close();
    		}
    		catch(SQLException e)
    		{
    			message = "Error executing the SQL statement: <br>" + e.getMessage();
    		}
    
    		HttpSession session = request.getSession();
    		session.setAttribute("message", message);
    		session.setAttribute("sqlStatement", sqlStatement);
    
    		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/sql_gateway.jsp");
    		dispatcher.forward(request, response);
    	}
    
    	public void doPost(HttpServletRequest request,
    					   HttpServletResponse response)
    					   throws IOException, ServletException
    	{
    		doGet(request, response);
    	}
    }
    Is the application getting the parameter from my jsp or am I overlooking something.
    Thanks for any assistance in this area. It would be nice to have some sort of debugger for this stuff. Developing in C I can step through line by line. Don’t know how to do that using Java 


    Best regards,

    Steve…

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: NullPointerException at doGet line 63

    Hi,

    When using request.getParamtere() you should always check for null, ie:

    Code:
    String sqlStatement = request.getParameter("sqlStatement");
    if(sqlStatement == null){
        // Handle this
    }
    It is good practice because you never know what users are gonna do over the web, they might call you servlet in some obscure way without parameters and then you get problems like this, at least if you know upfront that it is null you can handle it with less effort and you won't have any nasty crashes.

    As to a debugger you have a few options, the ones I use: NetBeans has a debugger built in. (You could also use other java ide's like Eclipse which I think has a debugger). Another option, write a class with a method that writes out to a file, then just add those lines all over the show, after executing your servlet go read the file.

    Hope This Helps
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  3. #3
    Join Date
    Jul 2006
    Posts
    29

    Re: NullPointerException at doGet line 63

    BNT

    Thanks for your response. I am currently putting some code in to check for null values. However, as I am new to Java, I am not sure how I am not able to pass the parameter from my jsp? Seeing that I am testing this myself I am always typing something in. Here is my code from my JSP that calls the servlet. Also I am kind of confused on the connection properties because the structure does not seem to call any particular database. I currently have MySQL, Oracle, and Microsoft SQL running on my machine and I have many different databases created.
    The “getConnection” values seem to just call for the URL, username, and password? How does it know what DB to use??

    Maybe I’m just missing one whole chapter

    Thanks again

    Steve….

    values from my web.xml file are commented the following is an excerpt from my servlet.

    Code:
    // jdbcDriver = net.sourceforge.jtds.jdbc.Driver
       dbDriver = config.getInitParameter("jdbcDriver");
    // dbUrl = jdbc:jtds:sqlserver://SMBELOW2006:8080
       dbURL = config.getInitParameter("dbUrl");
    // username = operator
       dbUser = config.getInitParameter("username");
    // password = operator
       dbPassword = config.getInitParameter("password");

    JSP form
    Code:
    <form action="../servlet/SQLGatewayServlet" method="post">
    	<b>SQL Statement</b><br />
    	<textarea name="sqlStatement" cols="60" rows="8">
    		<%= sqlStatement %>
    	</textarea>
    	<br /><br />
    	<input type="submit" value="Execute" />
    </form>

  4. #4
    Join Date
    Jul 2006
    Posts
    29

    Re: NullPointerException at doGet line 63

    OK, OK,
    I wasn’t looking at what tomcat was spitting out just the HTTP Status 500 thingy. Here is the message I’m getting from tomcat:

    Code:
    Error Opening the DB connection: Logon failed. Message 18456, stuff here
    Logon failed for user ‘operator’., server SMBELOW/SQLEXPRESS. Procedure, Line 1
    Now, I know I have this database set up and all but I will start playing with some other accounts I have. The same question above still exists though: How do I set a particular database scheme?

  5. #5
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: NullPointerException at doGet line 63

    It is possible then that your Connection object is null, do you get any errors when creating the connection? On the line 63 where you call connection.createStatement, that is where the NullPointer might be coming from, check your connection for null.

    When you say DriverManager.getConnection, you pass in the URL to your database, that is how it knows what databse to use. And when you say Class.forName("dbDrivers"), that is when you load the specific jdbc drivers for the product you are using.

    So which db are using? This will determine what drivers you need to use. You can also set up an odbc connection and use the jdbc -> odbc bridge instead of any specific driver to connect (although you will still need the odbc driver installed on your machine but sometimes they come with the os as standard). What is the database URL you are using, the one you have commented out "dbUrl = jdbc:jtds:sqlserver://SMBELOW2006:8080" doesn't look correct at all.

    Read THIS jdbc tutorial from Sun for more info on using jdbc.

    Sorry but I'm leaving the office now, will help you in the morning if you have any further issues.
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  6. #6
    Join Date
    Jul 2006
    Posts
    29

    Re: NullPointerException at doGet line 63

    BNT,
    Again thanks for the insight you are offering. I believe I am narrowing the focus to the connection properties. I am putting the JDBC drivers in my WEB-INF/lib folder. But, I don’t know if I have the latest and greatest for each database that I am using. Hopefully, not being annoying and all but is there a web site that is more user friendly when it comes to downloading the latest drivers? It seems as I do a search for anything and you get re-directed to all sorts of links. I just want the download page.

    I will also look into the link you provided. Thanks a whole lot and anything else you can think of will be received with the utmost attention!

    Best regards,

    Steve…

  7. #7
    Join Date
    Jul 2006
    Posts
    29

    Re: NullPointerException at doGet line 63

    In regards to this statement:

    Code:
    What is the database URL you are using, the one you have commented out "dbUrl = jdbc:jtds:sqlserver://SMBELOW2006:8080" doesn't look correct at all.
    The actual lines are in my web.xml file and are defined:

    Code:
    <init-param>
    	<param-name>dbUrl</param-name>
    	<param-value>jdbc:jtds:sqlserver://SMBELOW2006:8080”</param-value>
    </init-param>
    I’m now trying to find a suitable driver for the different databases (SQL, MySQL, etc)

    Code:
     
    <init-param>
    	<param-name>jdbcDriver</param-name>
    	<param-value>net.sourceforge.jtds.jdbc.Driver</param-value>
    </init-param>
    Working with this side of things seems to be where I need to camp?

  8. #8
    Join Date
    Jul 2006
    Posts
    29

    Re: NullPointerException at doGet line 63

    Just to keep people apprised of my progress:

    Setting my web.xml file as follows:

    Code:
    <init-param>
    	<param-name>jdbcDriver</param-name>
    	<param-value>net.sourceforge.jtds.jdbc.Driver</param-value>
    </init-param>
    <init-param>
    	<param-name>dbUrl</param-name>
    	<param-value>jdbc:jtds:sqlserver://SMBELOW2006/MyWeb11</param-value>
    </init-param>
    <init-param>
    	<param-name>username</param-name>
    	<param-value>operator</param-value>
    </init-param>
    <init-param>
    	<param-name>password</param-name>
    	<param-value>operator</param-value>
    </init-param>
    I only get a logon error, so, I would assume that the driver is working. The user and password above belongs to a particular database. Now I think the only question left is how do I establish the connection to a particular database? Of course I still need to hunt the drivers down for MySQL and Oracle. But if I could just get past my selection for a particular database I would be a happy person….

  9. #9
    Join Date
    Jul 2006
    Posts
    29

    Re: NullPointerException at doGet line 63

    Now we’re cooking with butter!!!!!! Ye haaaaa!

    Here are the changes I made to my web.xml:

    Code:
    <init-param>
    	<param-name>jdbcDriver</param-name>
    	<param-value>net.sourceforge.jtds.jdbc.Driver</param-value>
    </init-param>
    <init-param>
    	<param-name>dbUrl</param-name>
    	<param-value>jdbc:jtds:sqlserver://SMBELOW2006/</param-value>
    </init-param>
    <init-param>
    	<param-name>username</param-name>
    	<param-value>operator</param-value>
    </init-param>
    <init-param>
    	<param-name>password</param-name>
    	<param-value>operator</param-value>
    </init-param>
    <init-param>
    	<param-name>dbName</param-name>
    	<param-value>MyDatabase</param-value>
    </init-param>
    And the changes I made to my servlet SQLGatewayServlet.java:

    Code:
    
    dbDriver = config.getInitParameter("jdbcDriver");
    dbURL = config.getInitParameter("dbUrl");
    dbUser = config.getInitParameter("username");
    dbPassword = config.getInitParameter("password");
    dbName = config.getInitParameter("dbName");
    	
    try
    {
    	Class.forName(dbDriver);
    	connection = DriverManager.getConnection(dbURL + dbName,dbUser,dbPassword);
    }
    Look at the previous posts to see where I was going astray!!

    Big Smile…for now…but I’m sure I’ll be back

    Thanks for the help

    Steve…

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured