Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Important

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Important

    I don't know what wrong with my code. can anyone help me.

    Error:
    Exception in thread "main" java.lang.NullPointerException
    at InToPost$LinkList.deleteFront(InToPost.java:57)
    at InToPost.pop(InToPost.java:26)
    at InToPost.evaluate(InToPost.java:166)
    at InToPost.main(InToPost.java:210)

    My Code :
    import javax.swing.JOptionPane;
     
    public class InToPost {
    	private LinkList list;
     
    	private String inString;
     
    	private String outString = "";
     
    	public InToPost myStack;
     
    	public InToPost(String in) {
    		inString = in;
    		myStack = new InToPost();
    	}
     
    	public InToPost() {
    		list = new LinkList();
    	}
     
    	public void push(char j) {
    		list.insertFront(j);
    	}
     
    	public char pop() {
    		return list.deleteFront();
    	}
     
    	public boolean isEmpty() {
    		return (list.isEmpty());
    	}
     
    	public void displayStack() {
    		System.out.print("Stack: ");
    		list.displayList();
    	}
     
    class LinkList {
    	private Link head;
     
    	public LinkList() {
    		head = null;
    	}
     
    	public boolean isEmpty() {
    		return (head == null);
    	}
     
    	public void insertFront(char d) {
    		Link newLink = new Link(d);
    		newLink.next = head;
    		head = newLink;
    	}
     
    	public char deleteFront() {
    		Link temp = head;
    		head = head.next;
    		return temp.data;
    	}
     
    	public void displayList() {
    		Link current = head;
    		while (current != null) {
    			current.displayLink();
    			current = current.next;
    		}
    		System.out.println("");
    	}
     
    	class Link {
    		public char data; // data item
     
    		public Link next; // next link in list
     
    		public Link(char d) {
    			data = d;
    		}
     
    		public void displayLink() {
    			System.out.print(data + " ");
    		}
    	}
    }
    public String doTrans() {
    	for (int j = 0; j < inString.length(); j++) {
    		char ch = inString.charAt(j);
    		switch (ch) {
    			case '+': 
    			case '-':
    				gotOper(ch, 1); 
    				break; //   (precedence 1)
    			case '*': // it's * or /
    			case '/':
    				gotOper(ch, 2); // go pop operators
    				break; //   (precedence 2)
    			case '(': // it's a left paren
    				myStack.push(ch); // push it
    				break;
    			case ')': // it's a right paren
    				gotParen(ch); // go pop operators
    				break;
    			default: // must be an operand
    				outString = outString + ch; // write it to output
    				System.out.println(outString);
    				break;
    		}
    	}
     
    	while (!myStack.isEmpty()) {
    		outString = outString + myStack.pop();
    	}
    	System.out.println(outString);
    	return outString; // return postfix
    }
    public void evaluate() {
    	char a;
    	char b;
    	int c;
    	int d;
    	int total;
    	for (int j = 0; j < outString.length(); j++) {
    		char result = 0;
    		char ch = outString.charAt(j);
    		switch (ch) {
    			case '+':
    				a = myStack.pop();
    				c = Character.getNumericValue(a);
    				b = myStack.pop();
    				d = Character.getNumericValue(b);
    				total = c + d;
    				result =(char)total;
    				myStack.push(result);
    				break;
    			case '-':
    				a = myStack.pop();
    				c = Character.getNumericValue(a);
    				b = myStack.pop();
    				d = Character.getNumericValue(b);
    				total = c - d;
    				result =(char)total;
    				myStack.push(result);
    				break;
    			case '*':
    				a = myStack.pop();
    				c = Character.getNumericValue(a);
    				b = myStack.pop();
    				d = Character.getNumericValue(b);
    				total = c * d;
    				result =(char)total;
    				myStack.push(result);
    				break;
    			case '/':
    				a = myStack.pop();
    				c = Character.getNumericValue(a);
    				b = myStack.pop();
    				d = Character.getNumericValue(b);
    				total = c / d;
    				result =(char)total;
    				myStack.push(result);
    				break;
    			default:
    				myStack.push(ch);
    				break;
    		}
    	}
    	System.out.println(myStack.pop());
    }
     
    public void gotOper(char opThis, int prec1) {
    	while (!myStack.isEmpty()) {
    		char opTop = myStack.pop();
    		if (opTop == '(') {
    			myStack.push(opTop);
    			break;
    		}// it's an operator
    		else {// precedence of new op
    			int prec2;
    			if (opTop == '+' || opTop == '-')
    				prec2 = 1;
    			else
    			prec2 = 2;
    			if (prec2 < prec1) // if prec of new operator is less
    			{ //    than prec of old
    				myStack.push(opTop); // save newly-popped operator
    				break;
    			} else
    			// prec of new operator is not less than prec of old
    			outString = outString + opTop; 
    		}
    	}
    	myStack.push(opThis);
    }
    public void gotParen(char ch){ 
    	while (!myStack.isEmpty()) {
    		char chx = myStack.pop();
    		if (chx == '(') 
    			break; 
    		else
    		outString = outString + chx; 
    	}
    }
    public static void main(String[] args) {
    	String inString = JOptionPane.showInputDialog("Enter your expression: ");
    	System.out.println(inString);
    	String outString;
    	InToPost theTrans = new InToPost(inString);
    	outString = theTrans.doTrans();
    	System.out.println("Postfix is " +outString+ '\n');
    	InToPost calculate = new InToPost(outString);
    	calculate.evaluate();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Important

    Exception in thread "main" java.lang.NullPointerException
    at InToPost$LinkList.deleteFront(InToPost.java:57)
    Look at line 57 in your code. What variable has a null value there? Back track in the code to see why it does NOT have a valid value.
    If you can't see what variable, print out the values of all the variables on that line to see which is null

    Please edit your code and wrap it in code tags. See: BB Code List - Java Programming Forums or use Go Advanced and use the # icon

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Important

    i know where is the problem.
    but i cant to solve it logically in my mind.
    its so much important for me to solve it can anyone help me directly to re: edit my code.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Important

    i know where is the problem.
    Please explain.

    Did you answer these questions in post#2?
    Look at line 57 in your code. What variable has a null value there? Back track in the code to see why it does NOT have a valid value.
    If you can't see what variable, print out the values of all the variables on that line to see which is null

    important for me to solve it
    Ok, we're trying to help you solve the problem. But you need to find out what variable is null and why.

    If you are having a problem expressing your question in English, try using Google's translator program to convert from your language.
    Last edited by Norm; August 3rd, 2011 at 01:48 PM.

Similar Threads

  1. Blocking format (important)
    By benglish in forum Java Theory & Questions
    Replies: 3
    Last Post: April 11th, 2011, 06:34 PM
  2. Replies: 0
    Last Post: March 14th, 2010, 07:33 AM
  3. Important: Marking your Thread as SOLVED
    By JavaPF in forum Forum Updates & Feedback
    Replies: 0
    Last Post: December 5th, 2008, 09:33 AM