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) { }
}
}
# 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);
# 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);
}