Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Code Error

  • 13-03-2009 6:56pm
    #1
    Registered Users, Registered Users 2 Posts: 269 ✭✭


    I have developed a number of classes that i intend to expose as web services however i am getting a null pointer exception error help. I am having trouble seeing the error

    Exception in thread "main" java.lang.NullPointerException
    at ie.travel.databaseutility.DatabaseUtility.selectRecord(DatabaseUtility.java:69)
    at ie.travel.employee.Department.searchDepartmentName(Department.java:58)
    at ie.travel.webservices.LookupService.getDepartmentDetailsName(LookupService.java:24)
    at test.main(test.java:14)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)

    public class test {
    public static void main(String[]args){
        LookupService service = new LookupService();
        String result= service.getDepartmentDetailsName("jj");
        System.out.println(result);
    }
    }
    
    public class LookupService {
    private void LookupService(){
    }
    
    public String getDepartmentDetailsName(String DeptName)
    {
     
     Department mydepartment = new Department();
     mydepartment.setDeptName(DeptName);
     String departmentdetails = mydepartment.searchDepartmentName();
     return departmentdetails;
        
    }
    }
    
    public class Department {
    private String DeptName;
    private String DeptHead;
    private String DeptTel;
    private String DeptEmail;
    
    private void Department(){
    
    }
    
        public String getDeptEmail() {
            return DeptEmail;
        }
    
        public void setDeptEmail(String DeptEmail) {
            this.DeptEmail = DeptEmail;
        }
    
        public String getDeptHead() {
            return DeptHead;
        }
    
        public void setDeptHead(String DeptHead) {
            this.DeptHead = DeptHead;
        }
    
        public String getDeptName() {
            return DeptName;
        }
    
        public void setDeptName(String DeptName) {
            this.DeptName = DeptName;
        }
    
        public String getDeptTel() {
            return DeptTel;
        }
    
        public void setDeptTel(String DeptTel) {
            this.DeptTel = DeptTel;
        }
    
        public String searchDepartmentName()
        {
            DatabaseUtility traveldatabase = new DatabaseUtility();
            String result = traveldatabase.selectRecord("SELECT DeptHead FROM Department WHERE DeptName"+getDeptName());
            return result;
        }
    
    public class DatabaseUtility {
     Connection con;
     
     private void DatabaseUtility(){
         
         String username="root";
         String password="admin";
         	try{
    			Class.forName("com.mysql.jdbc.Driver");
    			}
    		catch(ClassNotFoundException e){
               String error= "Error Loading Driver" + e;
               System.out.println(error);
    		}
    		
    		try{
    			String url="jdbc:mysql:localhost:3306/Travel";
    			con=DriverManager.getConnection(url,username,password);
                con.setAutoCommit(false);
    		}
    		
    		catch(Exception e)
    		{
    			System.out.println("Error" + e);
            }
         
     }
       
     public String selectRecord(String selectquery)
     {
       try{
           Statement statement = con.createStatement();
           ResultSet result;
           result = statement.executeQuery(selectquery);
          int i = 0;
           while(result.next())
    		 {
               i = i+1;
    		   String queryresult= result.getString(i);
               return queryresult;
     		 }
    
       }
       catch (Exception e){
           try
           {
               con.rollback();
           }
           catch(SQLException sql)
           {
               String error = "SQL Error"+sql;
               return error;
           }
       }
       finally{
           try
           {
               con.close();
           }
           catch (SQLException sql)
           {
             String error = "SQL Error" +sql;
             return error;
           }
       }
      return "Unknown Problem Has Occurred";
     }
    }
    
    


Comments

  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    what variable type is DeptName in your database? i'm assuming it's a string of some sort. if so you need to structure your SQL statement so that it includes 'single quotes' around the DeptName you are using in your where clause.


  • Closed Accounts Posts: 24 colonelx


    Your constructor for DatabaseUtility is defined incorrectly.
    It should be public DatabaseUtility(){.....
    Then the code to create the Connection will be invoked.

    However, it seems like you were making an attempt to have DatabaseUtility defined as a singleton (hence the private constructor)
    You need to have an private instance of the class held inside DatabaseUtility.

    Have a look here http://www.javabeat.net/tips/7-singleton-pattern-design-patterns-in-javaj.html for some further info , or google java singleton pattern for more info.


  • Registered Users, Registered Users 2 Posts: 269 ✭✭cyberwit


    Did not realise i made the database Utility method private changed it however i am still getting the same errors
    Line 59 Department class:
          String result = traveldatabase.selectRecord("SELECT DeptHead FROM Department WHERE DeptName = "+"'"+deptname+"'"+";");
    
    Line 25 Lookup Service:
     String departmentdetails = mydepartment.searchDepartmentName();
    
    Line 59 DatabaseUtility:
        con.close();
    
    Line 15 Test:
       String result= service.getDepartmentDetailsName(data);
    
    

    There seems to be something wrong with my method calls. I wonder could it be to do with the MYSQL related code its my first time using MYSQL i have used Access and Oracle without a problem before. I am using XAMPP for my MYSQL D/B
    
    public class test {
    public static void main(String[]args){
        LookupService service = new LookupService();
        String data = "jj";
        String result= service.getDepartmentDetailsName(data);
        System.out.println(result);
    }
    }
    
    
    public class LookupService {
    
    public void LookupService(){
    }
    
    public String getDepartmentDetailsName(String DeptName)
    {
     
     Department mydepartment = new Department();
     
     mydepartment.setDeptName(DeptName);
     String departmentdetails = mydepartment.searchDepartmentName();
     return departmentdetails;    
    }
    }
    
    public class Department {
    private String DeptName;
    private String DeptHead;
    private String DeptTel;
    private String DeptEmail;
    
    public void Department(){
    
    }
    
        public String getDeptEmail() {
            return DeptEmail;
        }
    
        public void setDeptEmail(String DeptEmail) {
            this.DeptEmail = DeptEmail;
        }
    
        public String getDeptHead() {
            return DeptHead;
        }
    
        public void setDeptHead(String DeptHead) {
            this.DeptHead = DeptHead;
        }
    
        public String getDeptName() {
            return DeptName;
        }
    
        public void setDeptName(String DeptName) {
            this.DeptName = DeptName;
        }
    
        public String getDeptTel() {
            return DeptTel;
        }
    
        public void setDeptTel(String DeptTel) {
            this.DeptTel = DeptTel;
        }
    
        public String searchDepartmentName()
        {
            DatabaseUtility traveldatabase = new DatabaseUtility();
            String deptname = getDeptName();
            String result = traveldatabase.selectRecord("SELECT DeptHead FROM Department WHERE DeptName = "+"'"+deptname+"'"+";");
            return result;
         
            
        }
    
    }
    
    public class DatabaseUtility {
    
        Connection con;
    
        public void DatabaseUtility() {
    
            String username = "root";
            String password = "admin";
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                String error = "Error Loading Driver" + e;
                System.out.println(error);
            }
    
            try {
                String url = "jdbc:mysql:localhost:3306/Travel";
                con = DriverManager.getConnection(url, username, password);
                con.setAutoCommit(false);
            } catch (Exception e) {
                System.out.println("Error" + e);
            }
    
        }
    
        public String selectRecord(String selectquery) {
            try {
                Statement statement = con.createStatement();
                ResultSet result;
                result = statement.executeQuery(selectquery);
                int i = 0;
                while (result.next()) {
                    i = i + 1;
                    String queryresult = result.getString(i);
                    return queryresult;
                }
    
            } catch (Exception e) {
                try {
                    con.rollback();
                } catch (SQLException sql) {
                    String error = "SQL Error" + sql;
                    return error;
                }
            } finally {
                try {
                    con.close();
                } catch (SQLException sql) {
                    String error = "SQL Error" + sql;
                    return error;
                }
            }
            return "Unknown Problem Has Occurred";
        }
    }
    
    


  • Registered Users, Registered Users 2 Posts: 21,611 ✭✭✭✭Sam Vimes


    I don't think colonelx was referring to the fact that the constructor was private, he was pointing out it shouldn't have a return type. Get rid of void


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    Sam Vimes wrote: »
    I don't think colonelx was referring to the fact that the constructor was private, he was pointing out it shouldn't have a return type. Get rid of void

    Following on from the above point.. By declaring your constructor to have a return type, it is no longer a constructor and is now a method and therefore is not called when you instantiate the class. Therefore when you call the selectRecord method the connection is null, thus throwing the NPE.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 269 ✭✭cyberwit


    lynchie wrote: »
    Following on from the above point.. By declaring your constructor to have a return type, it is no longer a constructor and is now a method and therefore is not called when you instantiate the class. Therefore when you call the selectRecord method the connection is null, thus throwing the NPE.

    My constructor is just a constructor it does not return any values nor should it as that is the responsability of selectRecord(). my constructor just sets up connection
    
    package ie.travel.databaseutility;
    
    import java.sql.*;
    
    /**
     *
     * @author Organ
     */
    public class DatabaseUtility {
    
        Connection con;
    
        public void DatabaseUtility() {
    
            String username = "root";
            String password = "admin";
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                //String error = "Error Loading Driver" + e;
               // System.out.println(error);
            }
    
            try {
                String url = "jdbc:mysql://localhost:3306///Travel";
                con = DriverManager.getConnection(url, username, password);
                con.setAutoCommit(false);
            } catch (Exception e) {
                //System.out.println("Error" + e);
            }
    
        }
    
        public String selectRecord(String selectquery) {
            try {
                Statement statement = con.createStatement();
                ResultSet result;
                result = statement.executeQuery(selectquery);
                int i = 0;
                while (result.next()) {
                    i = i + 1;
                    String queryresult = result.getString(i);
                    return queryresult;
                }
    
            } catch (Exception e) {
                try {
                    con.rollback();
                } catch (SQLException sql) {
                    String error = "SQL Error" + sql;
                    return error;
                }
            } finally {
                try {
                    con.close();
                } catch (SQLException sql) {
                    String error = "SQL Error" + sql;
                    return error;
                }
            }
            return "Unknown Problem Has Occurred";
        }
    }
    


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    cyberwit wrote: »
    My constructor is just a constructor it does not return any values nor should it as that is the responsability of selectRecord(). my constructor just sets up connection

    please re-read my post. You do NOT have any constructors in your code!

    http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html


  • Registered Users, Registered Users 2 Posts: 21,611 ✭✭✭✭Sam Vimes


    lynchie wrote: »
    please re-read my post. You do NOT have any constructors in your code!

    http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html

    To reiterate, by putting the word void in your constructor you stop it being a constructor

    It should not be public void DatabaseUtility()

    It should be public DatabaseUtility()


Advertisement