Getting Java.Lang.NullPointerException

Hi,

I am getting java.lang.NullPointerException when calling the 'validate' method. Appreciate any help.

Thanks

import com.sun.net.ssl.internal.ssl.Provider;

import java.io.*;

import java.net.URL;

import java.net.URLEncoder;

import java.security.Security;

import java.text.DateFormat;

public class PSCASClient{

public static String validate(String fullUrl, String ticket, String altVal) throws IOException {

String validateURL = null;

if (altVal.equals("N")) {

validateURL = "https://login.uconn.edu/cas/validate";

}

else

{

validateURL = "https://login.uconn.edu/cas/validate";

}

try

{

Security.addProvider(new Provider());

System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

int ticketParamIndex = fullUrl.lastIndexOf("&ticket=");

String service = null;

if (ticketParamIndex != -1) {

// service = URLEncoder.encode(fullUrl.substring(0, ticketParamIndex)); /* This method is deprecated - JA */

service = URLEncoder.encode(fullUrl.substring(0, ticketParamIndex), service);

}

else {

// service = URLEncoder.encode(fullUrl); /* This method is deprecated - JA */

service = URLEncoder.encode(fullUrl, service);

}

URL u = new URL(validateURL + "?ticket=" + ticket + "&service=" + service);

BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));

if (in == null) {

return null;

}

String line1 = in.readLine();

String line2 = in.readLine();

if (line1 != null && line2 != null && line1.equals("yes"))

{

String user = line2;

user = user.toUpperCase();

return user;

}

else {

return null;

}

}

catch(Exception e)

{

logMessage("ERROR: Exception attempting validate: " + e);

}

return null;

}

public static void logMessage(String message){

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");

Date now = new Date();

try

{

BufferedWriter os = new BufferedWriter(new FileWriter("/tmp/pscas_signon_log.txt", true));

os.write(formatter.format(now));

os.write(message);

os.write("\n");

os.close();

}

catch(IOException _ex) { }

}

}

[2445 byte] By [Jay_Amballaa] at [2007-11-26 18:26:57]
# 1
Please use code tags. And which line, exactly, is producing the NullPointerException (not a line number, but the actual line).Edit:And what made you decide to post this in the JSSE forum.
masijade.a at 2007-7-9 6:01:03 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 2

> Hi,

> I am getting java.lang.NullPointerException when

> calling the 'validate' method. Appreciate any help.

>

> Thanks

>

>

> import com.sun.net.ssl.internal.ssl.Provider;

> import java.io.*;

> import java.net.URL;

> import java.net.URLEncoder;

> import java.security.Security;

> import java.text.DateFormat;

>

> public class PSCASClient{

>

> public static String validate(String fullUrl, String

> ticket, String altVal) throws IOException {

>

> String validateURL = null;

> if (altVal.equals("N")) {

> validateURL = "https://login.uconn.edu/cas/validate";

>

>

> }

> else

> {

> validateURL = "https://login.uconn.edu/cas/validate";

>

>

> }

>

> try

> {

> Security.addProvider(new Provider());

> System.setProperty("java.protocol.handler.pkgs",

> "com.sun.net.ssl.internal.www.protocol");

> int ticketParamIndex =

> fullUrl.lastIndexOf("&ticket=");

>

> String service = null;

> if (ticketParamIndex != -1) {

> // service = URLEncoder.encode(fullUrl.substring(0,

> ticketParamIndex)); /* This method is deprecated - JA

> */

> service = URLEncoder.encode(fullUrl.substring(0,

> ticketParamIndex), service);

> }

> else {

> // service = URLEncoder.encode(fullUrl); /* This

> method is deprecated - JA */

> service = URLEncoder.encode(fullUrl, service);

> }

>

> URL u = new URL(validateURL + "?ticket=" + ticket +

> "&service=" + service);

>

>

> BufferedReader in = new BufferedReader(new

> InputStreamReader(u.openStream()));

> if (in == null) {

> return null;

> }

>

> String line1 = in.readLine();

> String line2 = in.readLine();

>

> if (line1 != null && line2 != null &&

> line1.equals("yes"))

> {

> String user = line2;

> user = user.toUpperCase();

> return user;

> }

> else {

> return null;

> }

> }

> catch(Exception e)

> {

> logMessage("ERROR: Exception attempting validate: " +

> e);

> }

> return null;

> }

>

> public static void logMessage(String message){

>

> SimpleDateFormat formatter = new

> SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");

> Date now = new Date();

> try

> {

> BufferedWriter os = new BufferedWriter(new

> FileWriter("/tmp/pscas_signon_log.txt", true));

> os.write(formatter.format(now));

> os.write(message);

> os.write("\n");

> os.close();

> }

> catch(IOException _ex) { }

> }

>

> }

>

I am using the package jsse.jar. The following lines may be causing the error.

try

{

Security.addProvider(new Provider());

System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

int ticketParamIndex = fullUrl.lastIndexOf("&ticket=");

String service = null;

if (ticketParamIndex != -1) {

// service = URLEncoder.encode(fullUrl.substring(0, ticketParamIndex)); /* This method is deprecated - JA */

service = URLEncoder.encode(fullUrl.substring(0, ticketParamIndex), service);

}

else {

// service = URLEncoder.encode(fullUrl); /* This method is deprecated - JA */

service = URLEncoder.encode(fullUrl, service);

}

URL u = new URL(validateURL + "?ticket=" + ticket + "&service=" + service);

Jay_Amballaa at 2007-7-9 6:01:03 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 3
And, once again, which line, exactly, is causing the problem. The exception will contain a line number, find that line in your code and paste exactly that line into a new post here.
masijade.a at 2007-7-9 6:01:03 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 4

I am getting the error at run time, when I am calling the Java method from PeopleSoft Peoplecode. It does not give a line number. But the error is in the try-catch block. The error I am getting is --

ERROR: Exception attempting validate: Java.Lang.NullPointerException

the code from try-catch is given below. Thanks for looking into this.

try

{

Security.addProvider(new Provider());

System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

int ticketParamIndex = fullUrl.lastIndexOf("&ticket=");

String service = null;

if (ticketParamIndex != -1) {

// service = URLEncoder.encode(fullUrl.substring(0, ticketParamIndex)); /* This method is deprecated - JA */

service = URLEncoder.encode(fullUrl.substring(0, ticketParamIndex), service);

}

else {

// service = URLEncoder.encode(fullUrl); /* This method is deprecated - JA */

service = URLEncoder.encode(fullUrl, service);

}

URL u = new URL(validateURL + "?ticket=" + ticket + "&service=" + service);

BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));

if (in == null) {

return null;

}

String line1 = in.readLine();

String line2 = in.readLine();

logMessage("line1 = " + line1);

logMessage("line2 = " + line2);

if (line1 != null && line2 != null && line1.equals("yes"))

{

String user = line2;

user = user.toUpperCase();

return user;

}

else {

return null;

}

}

catch(Exception e)

{

logMessage("ERROR: Exception attempting validate: " + e);

}

Jay_Amballaa at 2007-7-9 6:01:03 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 5
Change your catch block to include e.printStackTrace();Then you will get your line number and finally get down to some real work.
masijade.a at 2007-7-9 6:01:03 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...