0

I'm trying to make a calculator app. First thing first I created a class that will take a string (my equation to solve) and change it from Infix to Postfix. The app is literally bear bones, but when I start it, it crashes immediately, and in the console I get no error. As anyone idea where might be the problem?

The manifest file:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.projectcalculator"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

XML file activity_main:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android.projectcalculator.MainActivity"> <TextView android:id="@+id/textCalc" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10sp" android:layout_weight="1" android:text="Hello"/> </LinearLayout> 

The java file MainActivity:

package com.example.android.projectcalculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import static android.R.attr.onClick; import com.example.android.projectcalculator.InfixToPostfix; public class MainActivity extends AppCompatActivity { public TextView calculationText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PrintMainScreen("hello you"); String s = InfixToPostfix.StartInfixToPostfix("A*(B+C)"); PrintMainScreen(s); } public void PrintMainScreen(String str) { TextView txview = (TextView)findViewById(R.id.textCalc); txview.setText(str); } } 

The Java file InfiToPostfix:

package com.example.android.projectcalculator; import java.util.Stack; public class InfixToPostfix{ //Varibili private private static String postfixOutput; private static Stack<Character> operatorStack; private static String infixInput; //Metodo per controlare se ho a che fare con l'operatore private static boolean IsOperator (char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '^'; } private static int OpratorPriority(Character operator1) { switch(operator1) { case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; default: return 0; } } //Metodo Supremo public static String StartInfixToPostfix(String in) { //inizializzo variabili postfixOutput = ""; infixInput.equals(in); int lunghezza = infixInput.length(); operatorStack = new Stack<Character>(); //inizio il processo for (int i=0; i < infixInput.length(); i++) { //se non è un operatore ma un operando, lo aggiungo alla string di output if (!IsOperator(infixInput.charAt(i))) { postfixOutput += infixInput.charAt(i); } //Considero il caso in cui sia l'operatore ')' else if (infixInput.charAt(i) == ')') { //Inserisco nel postfix gli operatori fino a che lo sctack è vuoto o incontro una parentesi chiusa while (!operatorStack.isEmpty() && operatorStack.peek() != ')') { postfixOutput += (operatorStack.pop()); } //elimino la '(' se c'è if (!operatorStack.isEmpty()) { operatorStack.pop(); } } //considero il caso in cui ho un operatore che non sia ')' else { //questo while si attiva solo se (1) lo stack non è vuoto (2) l'elemento in cima allo stack non è '(' (3) se l'ultimo operatore ha grado minore while ( (!operatorStack.isEmpty()) && (operatorStack.peek() != '(') && (OpratorPriority(operatorStack.peek()) >= OpratorPriority(infixInput.charAt(i)))) { postfixOutput += operatorStack.pop(); } //aggiungo l'operatore a prescindere di ciò che ho fatto o non fatto con il cilo while operatorStack.push(infixInput.charAt(i)); } } //Alla fine del metodo rilascio il postfix return postfixOutput; } } 

Edit: Has anyone idea why I get error if everything in the InfixToPostfix class is not static?

Edit2: Now is giving me this error...

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.projectcalculator, PID: 25981 Theme: themes:{} java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.projectcalculator/com.example.android.projectcalculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at com.example.android.projectcalculator.InfixToPostfix.StartInfixToPostfix(InfixToPostfix.java:40) at com.example.android.projectcalculator.MainActivity.onCreate(MainActivity.java:20) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5461)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
3
  • if you say crash then there must a log that you should post. Your activity code looks alright Commented Oct 18, 2016 at 17:24
  • infixInput is null. Commented Oct 18, 2016 at 17:28
  • A swear that the console didn't gave me any error the first 3 or 4 times... Now it give me this errors. Commented Oct 18, 2016 at 17:29

3 Answers 3

1

your infixInput in line 40 is null as it is not initialized

Sign up to request clarification or add additional context in comments.

4 Comments

As a first thing I declare 'infixInput.equal(in);'
Nope this check if infixInput hold same value as in. It does not initialize anything !
How can I read that gibberish, and understand that that infix Input is null??
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference StartInfixToPostfix(InfixToPostfix.java:40) Which means you tried to use equals on a null object..
1

you should initialize your infixInput variable.

private static String infixInput = ""; 

Also it seems you wanted to initialize infixInput with in value. Doing infixInput.equals(in) just check if both variables hold same value and do return a boolean.

Then you should do :

infixInput = in.toString() 

Comments

1

infixInput.equals(in); is NULL. You have to initialize infixInput first.

String.equals(String) checks if the content of both strings is equal. This doesn't work if one of the Strings is null.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.