Mr. Robot
Mr. Robot

Reputation: 605

FATAL EXCEPTION: main [Android developing]

At first I'm sorry for my bad English.

The problem: I compile this code (MyActivity.java):

package com.bundas.tdsestr.kalkulacka;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;


public class MyActivity extends Activity {
    private int[] array = new int[19999];
    private String[] text_na_displeji = new String[19999];
    private int postup = 0;
    private TextView Display;
    private Button tlacitko1, tlacitko2, tlacitko3, tlacitko4, tlacitko5, tlacitko6, tlacitko7, tlacitko8, tlacitko9, rovnaSe, plus, minus;
    public String vysledek = "";
    private int konecnyVysledek=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        this.tlacitko1 = (Button)findViewById(R.id.tlacitko1);
        this.tlacitko2 = (Button)findViewById(R.id.tlacitko2);
        this.tlacitko3 = (Button)findViewById(R.id.tlacitko3);
        this.tlacitko4 = (Button)findViewById(R.id.tlacitko4);
        this.tlacitko5 = (Button)findViewById(R.id.tlacitko5);
        this.tlacitko6 = (Button)findViewById(R.id.tlacitko6);
        this.tlacitko7 = (Button)findViewById(R.id.tlacitko7);
        this.tlacitko8 = (Button)findViewById(R.id.tlacitko8);
        this.tlacitko9 = (Button)findViewById(R.id.tlacitko9);
        this.rovnaSe = (Button)findViewById(R.id.tlacitkoRovnaSe);
        this.plus = (Button)findViewById(R.id.tlacitkoPlus);
        this.minus = (Button)findViewById(R.id.tlacitkoMinus);

        tlacitko1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text_na_displeji[postup] = "1";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        tlacitko2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                text_na_displeji[postup] = "2";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        tlacitko3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                text_na_displeji[postup] = "3";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        tlacitko4.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                text_na_displeji[postup] = "4";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        tlacitko5.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                text_na_displeji[postup] = "5";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        tlacitko6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text_na_displeji[postup] = "6";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        tlacitko7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text_na_displeji[postup] = "7";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        tlacitko8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text_na_displeji[postup] = "8";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        tlacitko9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text_na_displeji[postup] = "9";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        rovnaSe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean poprve = false;
                boolean poprveZnak = false;
                boolean plus = true;
                int temp = 0;
                int tempCislo1 = 0;
                int tempCislo2 = 0;


                for(int i =0; i < text_na_displeji.length; i++){
                    if(text_na_displeji[i] == "+" || text_na_displeji[i] == "-"){
                        if(poprveZnak == false){
                            for(int j = 0; j < i-1; j++){
                                tempCislo1 += array[j];
                            }
                            poprveZnak = true;
                            temp = i;
                            if(text_na_displeji[i] == "+"){
                                plus = true;
                            }
                            else plus = false;
                        }

                        else if(poprveZnak == true){
                            for(int j = temp; j < i-1; j++){
                                tempCislo2 += array[j];
                            }
                            temp = i;
                            if(plus == true){
                                konecnyVysledek += (tempCislo1+tempCislo2);
                                tempCislo1 = 0;
                            }
                            else{
                                konecnyVysledek += (tempCislo1-tempCislo2);
                                tempCislo1 =0;
                            }
                            if(text_na_displeji[i] == "+") plus=true;
                            else plus=false;
                        }
                    }

                    else array[i] = Integer.parseInt(text_na_displeji[i]);

                }
                String temp22 = "Vysledek je: " + konecnyVysledek;
                Display.setText(temp22);
            }
        });

        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text_na_displeji[postup] = "+";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });

        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text_na_displeji[postup] = "-";
                postup++;
                for(int i=0; i < text_na_displeji.length; i++){
                    vysledek += text_na_displeji[i];
                    Display.setText(vysledek);
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

And when I click on, for example, number one (tlacitko1 (in my language)), my program crashes.

This is the output in logcat:

    07-18 22:33:26.416  28199-28199/com.bundas.tdsestr.kalkulacka D/dalvikvm﹕ Late-enabling CheckJNI
07-18 22:33:26.496  28199-28199/com.bundas.tdsestr.kalkulacka W/Trace﹕ error opening trace file: No such file or directory (2)
07-18 22:33:26.936  28199-28199/com.bundas.tdsestr.kalkulacka D/libEGL﹕ loaded /system/lib/egl/libEGL_tegra.so
07-18 22:33:26.956  28199-28199/com.bundas.tdsestr.kalkulacka D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_tegra.so
07-18 22:33:26.956  28199-28199/com.bundas.tdsestr.kalkulacka D/libEGL﹕ loaded /system/lib/egl/libGLESv2_tegra.so
07-18 22:33:26.986  28199-28199/com.bundas.tdsestr.kalkulacka D/OpenGLRenderer﹕ Enabling debug mode 0
07-18 22:33:34.856  28199-28199/com.bundas.tdsestr.kalkulacka D/AndroidRuntime﹕ Shutting down VM
07-18 22:33:34.856  28199-28199/com.bundas.tdsestr.kalkulacka W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40dd2930)
07-18 22:33:34.856  28199-28199/com.bundas.tdsestr.kalkulacka E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.bundas.tdsestr.kalkulacka.MyActivity$9.onClick(MyActivity.java:140)
            at android.view.View.performClick(View.java:4211)
            at android.view.View$PerformClick.run(View.java:17362)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5227)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
            at dalvik.system.NativeStart.main(Native Method)

My XML CODE:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/tlacitko1"
        android:layout_marginLeft="162dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/tlacitko2"
        android:layout_marginLeft="37dp"
        android:layout_alignTop="@+id/tlacitko1"
        android:layout_toRightOf="@+id/tlacitko1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:id="@+id/tlacitko3"
        android:layout_marginLeft="37dp"
        android:layout_alignBottom="@+id/tlacitko2"
        android:layout_toRightOf="@+id/tlacitko2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"
        android:id="@+id/tlacitko4"
        android:layout_below="@+id/tlacitko1"
        android:layout_alignLeft="@+id/tlacitko1"
        android:layout_alignStart="@+id/tlacitko1"
        android:layout_marginTop="38dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        android:id="@+id/tlacitko5"
        android:layout_alignTop="@+id/tlacitko4"
        android:layout_alignLeft="@+id/tlacitko2"
        android:layout_alignStart="@+id/tlacitko2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        android:id="@+id/tlacitko6"
        android:layout_alignTop="@+id/tlacitko5"
        android:layout_alignLeft="@+id/tlacitko3"
        android:layout_alignStart="@+id/tlacitko3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"
        android:id="@+id/tlacitko7"
        android:layout_below="@+id/tlacitko4"
        android:layout_alignLeft="@+id/tlacitko4"
        android:layout_alignStart="@+id/tlacitko4"
        android:layout_marginTop="38dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        android:id="@+id/tlacitko8"
        android:layout_alignTop="@+id/tlacitko7"
        android:layout_alignLeft="@+id/tlacitko5"
        android:layout_alignStart="@+id/tlacitko5" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        android:id="@+id/tlacitko9"
        android:layout_alignTop="@+id/tlacitko8"
        android:layout_alignLeft="@+id/tlacitko6"
        android:layout_alignStart="@+id/tlacitko6" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:id="@+id/tlacitkoMinus"
        android:layout_below="@+id/tlacitko7"
        android:layout_alignRight="@+id/tlacitko8"
        android:layout_alignEnd="@+id/tlacitko8"
        android:layout_marginTop="47dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/tlacitkoPlus"
        android:layout_alignTop="@+id/tlacitkoMinus"
        android:layout_toLeftOf="@+id/tlacitko2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="
        android:id="@+id/tlacitkoRovnaSe"
        android:layout_alignTop="@+id/tlacitkoMinus"
        android:layout_alignLeft="@+id/tlacitko9"
        android:layout_alignStart="@+id/tlacitko9" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:id="@+id/display"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:textSize="30dp" />
</RelativeLayout>

Thank you so much for your time. I am 15-year-old and I am really bad programmer.. I primarily program in C/C++ for Linux...

Upvotes: 0

Views: 81

Answers (1)

Oleg Gryb
Oleg Gryb

Reputation: 5259

You need to assign something to Display before you can call methods from it, e.g.:

Display = (TextView)findViewById(R.id.display);

Upvotes: 1

Related Questions