简体   繁体   中英

NullPointerException in Web Services Client (eclipse)

I am fairly new java web services and I trying to use it to access an oracle database. What my project is trying to do is take the input of a zip code and return the information from the database.

I got a web services client working for an animal type using this tutorial and I am trying to take what I learned from that for my project: http://javapapers.com/web-service/java-web-service-using-eclipse/

Here is my the code for the main class I am using:

package com.zipws.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//import javax.jws.WebService;
//import javax.jws.soap.SOAPBinding;
//import javax.jws.soap.SOAPBinding.Style;

public class ZipWebServiceImpl {
public String cityFinder(String zip) {
    Connection con = null;         
    String str = "";

    try{
        String user = "IVRDEVUSR";
        String pass = "voice001";
        String url = "jdbc:oracle:thin:@UIQ-UAT-ORA-02:1521/IVRST01";
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.getConnection(url, user, pass);

        Statement stmt = con.createStatement();

        ResultSet rsZip = stmt.executeQuery("SELECT *FROM ZIPLOC WHERE ZIP = " + zip);

        while(rsZip.next()){
            if(zip.equals(rsZip.getString("ZIP"))){
                str = rsZip.getString("CITY");
            }
            else{
                str = "Zip code for city not found!";
            }

        }
        rsZip.close();
        stmt.close();
    }
    catch(SQLException e){
        //System.out.println("Connection Failed.");
        str = "connection failed";

    }
    catch(ClassNotFoundException cnfe){
        //System.out.println("Unable to load driver.");
        str = "Unable to load driver";
    }
    finally{
        try{
            con.close();
        }
        catch(SQLException e){
            //System.out.println("Failed to close connection.");
            str = "Failed to close connection.";
        }
    }
    return str;
}
}

The web services client classes were generated by Eclipse which I learned to do from the tutorial earlier. When I run the client and enter a zipcode to try to invoke the above class, it returns a NullPointerException and I do not know why. Can anyone possibly explain why?

You should check in the finally block if 'con != null'. Otherwise even in cases the driver could not be loaded you are trying to close the connection which can never succeed.

Debug the method and check what exception occurs. Is maybe the oracle driver missing in the classpath?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM