0

I have a problem here. When im inserting a query im getting a nullpointerexception. Im using jdk1.8, eclipse neon ee, postgresql9.4, tomcat8.5. It is stated in the exception that i have a problem on the execute method although my code is correct and it works on later versions of eclipse, postgresql8.1, tomcat7.

The Exception Error

This the code from the execute1.jsp

<%

        clsDatabaseManager obj = new clsDatabaseManager();

            obj.openConnection();

            String stud_id = request.getParameter("stud_id");
            String stud_name = request.getParameter("stud_name");
            String stud_password = request.getParameter("stud_password");
            String contact = request.getParameter("contact");
            String address = request.getParameter("address");
            String course = request.getParameter("course");
            String query = "INSERT INTO tbl_student(stud_id,stud_name,stud_password,contact,address,course) VALUES('"+stud_id+"','"+stud_name+"','"+stud_password+"','"+contact+"','"+address+"','"+course+"')";

            obj.execute(query); 

            obj.closeConnection();

            String site = new String("View.jsp");
            response.setStatus(response.SC_MOVED_TEMPORARILY);
            response.setHeader("Location", site); 


            //try to block to handle code that amy cause exception





        %>

Connectable.java Code

package finals.project;

import java.sql.SQLException;
import java.util.Vector;

public interface Connectable {

    void openConnection();
    void closeConnection() throws SQLException;
    boolean execute(String q) throws SQLException;
    String getSingleValue(String q) throws SQLException;
    boolean checkExist(String q) throws SQLException;
    Vector<String> getVectorList(String query, int itemCount) throws SQLException;
    Vector<String> getSingleVectorList(String query) throws SQLException;

}

And here is my clsDatabaseManager.java code because it is long here is the link https://www.dropbox.com/s/dc8be3jhuj9dr0e/clsDatabaseManager.java?dl=0

  • Have you checked the log? It seems the connection has not been initialized (i.e. is `null`), I would expect an exception from the `openConnection()` call. – Jozef Chocholacek Oct 14 '16 at 06:12
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Pradeep Simha Oct 14 '16 at 06:12
  • 2
    And 2 notes: First, **NEVER EVER** construct SQL queries from user provided parameters by concatenating strings. Have you heard about [SQL Injection](https://en.wikipedia.org/wiki/SQL_injection)? Use [PreparedStatement](http://software-security.sans.org/developer-how-to/fix-sql-injection-in-java-using-prepared-callable-statement) the proper way, not how you use it in your `clsDatabaseManager`. Second, don't put business logic into JSP - use MVC pattern, or at least put the logic into a helper class, and call the class methods. It will be - besides other advantages - much easier to debug. – Jozef Chocholacek Oct 14 '16 at 06:13
  • @JozefChocholacek i still didnt ill check now – Harold James Imperio Oct 14 '16 at 06:14
  • @JozefChocholacek yah i know about it. This code is just for the presentation for tomorrow that i dont need for a long term. The code is from my professor though when i used it on our lab it works but on my own computer it returns that exception – Harold James Imperio Oct 14 '16 at 06:17
  • @JozefChocholacek here are the log files https://www.dropbox.com/s/tx7usyhfp9sho3b/tomcat8-stderr.2016-10-07.log?dl=0 https://www.dropbox.com/s/4r0ypoksoqvqu64/catalina.2016-10-07.log?dl=0 – Harold James Imperio Oct 14 '16 at 06:20
  • @HaroldJamesImperio Hm, there are another exceptions, but not the one I would expect. So you have to run your Tomcat in debug mode, and trace the code (i.e. put breakpoints at the start of `openConnection` and `execute` methods, and watch line by line what's happening. – Jozef Chocholacek Oct 14 '16 at 06:31

0 Answers0