It's the calculation code for the four-day operation using Java generic stack, but the nullPointerException error keeps popping up

Asked 1 years ago, Updated 1 years ago, 94 views

It's a code that uses generic stacks I keep getting a nullpointerception error like that. Below is the full code. }

import java.util.*;

class GStack<T> {
static int top;
Object [] stck;

public GStack() {
  top=0;
  stck = new Object[200]; 
}

public T peek() {
  if(top==0)
     return null;
  return (T)stck[top];
}

public void push(T item) {
  if(top==200)
     return;
  stck[top]=item;
  top++;
}

public T pop() {
  if(top==0)  
      return null;
  top--;
  return (T)stck[top];
}      
}

public class fix {
private static int  PIS (String temp) {

  If(temp.equals("^") return 4; // I changed it from 2 because it looks like 4
  if(temp.equals("*")||temp.equals("/")) return 3;
  if(temp.equals("+")||temp.equals("-")) return 2;
  if(temp.equals(")")) return 1;
  if(temp.equals("(")) return 0;
  return 0;
}

private static int PIE (String temp) 
{

  if(temp.equals("(")) return 5; 
  if(temp.equals("^")) return 4;
  if(temp.equals("*")||temp.equals("/")) return 3;
  if(temp.equals("+")||temp.equals("-")) return 2;
  if(temp.equals(")")) return 1;
  if(temp.equals("=")||temp.equals(" "))return -1;
  return 0;
}

static String IntoPost(StringTokenizer st) 
{
  GStack<String> stringStack = new GStack<String>();

  int n=st.countTokens(); 

  ArrayList<String> a = new ArrayList<String>(n); 
  for(int i=0;i<n;i++) 
     {   
        String token=st.nextToken();
        try {
           Double.parseDouble(token);
           a.add(token);                
           System.out.print(token+" ");               

        } } catch(NumberFormatException e) { }

     switch(token) {         
     case ")" :             
           while(!stringStack.equals("("))
              { 
                a.add(stringStack.peek());                     
                System.out.print(stringStack.pop()+" ");                        
              }               
           stringStack.pop();//"(Remove remaining brackets)                        
           break;

     case "+" :case "*" : case "-" : case "/" : case "(": 
        if(PIS(stringStack.peek())>= (PIE(token)))
        {         
           while(stringStack.top!=0)
           {
                 a.add(stringStack.peek());               
              System.out.print(stringStack.pop()+" ");
           }            
        }                 
        stringStack.push(token);                
        break;

     case "=":             
        while(stringStack.top!=0)
        {               
          a.add(stringStack.peek());
          System.out.print(stringStack.pop()+" ");               
        }            
     }         
  }

  //ArrayList<String> Combine by separator
  String Postfix = String.join("",a);
  return Postfix;   
}

static double Evaluation(String s) {
  GStack<Double> DoubleStack = new GStack<Double>();      
  StringTokenizer st = new StringTokenizer(s," ");      
  int n=st.countTokens();
  double temp=0;
  double op1,op2;

  for(int i=0;i<n;i++) {
     String token = st.nextToken();
     if((token.equals("+")==false)&&(token.equals("-")==false)&&
           (token.equals("*")==false)&&(token.equals("/")==false)) 
     {
        temp=Double.parseDouble(token) ;
        DoubleStack.push(temp);
     }
     else {
        op2=DoubleStack.pop();
        op1=DoubleStack.pop();

        switch(token) {
        case "+" :  DoubleStack.push(op1+op2); break;
        case "-" : DoubleStack.push(op1-op2); break;
        case "*" : DoubleStack.push(op1*op2); break;
        case "/" : DoubleStack.push(op1/op2); break;
        }
     }
  }
  return temp;
}



public static void main(String[] args) {

  Scanner in= new Scanner(System.in);      
  System.out.print ("Enter a Formula");
  String Postfix = in.nextLine();
  StringTokenizer st = new StringTokenizer(Postfix,"+ *( /-)^=",true);
  String postFix= IntoPost(st);
  double result=   Evaluation(postFix);      
  System.out.print("Answer: "+ result");      
  in.close();
}

java stack generic calculate

2022-09-22 20:09

1 Answers

If you follow each line of code and read it, you can see where the null comes from.

Enter the formula when you meet and add 1+1

You can meet the same error that the questioner posted.

In the IntoPost function, in the switch statement

if (PIS(stringStack.peak()) >= (PIE(token))) If you look at this section

stringStack.When peak(), if top==0 inside, it is supposed to return null. Check whether the temp value is null or not when receiving the String temp parameter in the PIS function to prevent null Pointer from occurring.


2022-09-22 20:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.