Niyanta Niyanta - 1 year ago 82
Java Question

String Palindrome Program

I created a string palindrome program, but I am getting a Nullpointer exception.

Here is the code that I have written:

public class JavaApplication2 {

* @param args the command line arguments
char c[]={'i','c','i','c','i'};
String s = new String(c);
String s1=null;
int i,j;
char c1[];
public static void main(String[] args) {
//TODO code application logichere

JavaApplication2 ja=new JavaApplication2();
boolean b1= ja.ans();

public void palindrome()

public boolean ans()
String s2= new String(c1);
return true;
return false;


I can't figure out how to deal with it and exactly why this error is occuring.

Answer Source

The Problem is that char c1[]; is not inizalized. Therefore this line give you a nullpointer:

String s2 = new String(c1);

Inizalized the variable:

char c1[] = {};

Now the NullPointer is gone, but thats not the final version, you need to fill it with your logic.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download