-1

Error is:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{lol2dubs.stevemoa/lol2dubs.stevemoa.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

Code is:

public class MainActivity extends AppCompatActivity {
double MOA;
TextView turretClicks = (TextView)findViewById(R.id.turretClicks);
boolean noMOA;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    EditText range = (EditText) findViewById(R.id.rangeEntry);
    String stringRange = range.getText().toString();
    int finalRange = Integer.parseInt(stringRange);
    if(finalRange <= 200) {
        MOA = 0;
    }
    if(finalRange > 200 && finalRange <= 225){
        MOA = .5;
    }
    if(finalRange > 225 && finalRange <= 250) {
        MOA = 1;
    }
    if(finalRange > 250 && finalRange <= 275) {
        MOA = 1.65;
    }
    if(finalRange > 275 && finalRange <= 300) {
        MOA = 2.25;
    }
    if(finalRange > 300 && finalRange <= 325) {
        MOA = 2.8;
    }
    if(finalRange > 325 && finalRange <= 350) {
        MOA = 3.5;
    }
    if(finalRange > 350 && finalRange <= 375) {
        MOA = 4.0;
    }
    if(finalRange > 375 && finalRange <= 400) {
        MOA = 4.75;
    }
    if(finalRange > 400 && finalRange <= 425) {
        MOA = 5.50;
    }
    if(finalRange > 425 && finalRange <= 450) {
        MOA = 6.25;
    }
    if(finalRange > 450 && finalRange <= 475) {
        MOA = 7.0;
    }
    if(finalRange > 475 && finalRange <= 500) {
        MOA = 7.5;
    }
    if(finalRange > 500 && finalRange <= 525) {
        MOA = 8.25;
    }
    if(finalRange > 525 && finalRange <= 550) {
        MOA = 9.0;
    }
    if(finalRange > 550 && finalRange <= 575) {
        MOA = 9.75;
    }
    if(finalRange > 575 && finalRange <= 600) {
        MOA = 10.5;
    }
    if(finalRange > 600 && finalRange <= 625) {
        MOA = 11.5;
    }
    if(finalRange > 625 && finalRange <= 650) {
        MOA = 12.25;
    }
    if(finalRange > 650 && finalRange <= 675) {
        MOA = 13;
    }
    if(finalRange > 675 && finalRange <= 700) {
        MOA = 14;
    }
    if(finalRange > 700) {
       noMOA = true;
    }


    // Create an anonymous implementation of OnClickListener
    View.OnClickListener btnClickCalc = new View.OnClickListener() {


        public void onClick(View v) {
            double clicks = (MOA * 4);
            String toText = Double.toString(clicks);
            turretClicks.setText(toText);



        }
    };

        // Capture our button from layout
        Button button = (Button)findViewById(R.id.btnClickCalc);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(btnClickCalc);

    }
}

Basically, I'm making a simple app for my father in law so he doesn't have to calculate MOA on his rifle turrets. His scope is quarter MOA, so if he's shooting at 550 yards, he has to use 9 MOA, which is 9 sets of 4 clicks.

I have an Enter Range EditText named rangeEntry I have a button named btnClickCalc I have a TextView named turretClicks that should display the # of clicks required for said range.

What am I missing with the error? I see that it's because something is null... and I see other articles saying they didn't instantiate something... but I'm not seeing what my problem is.

Anyone?

XML is:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate MOA"
    android:id="@+id/textView"
    android:textColor="#ff0000"
    android:textSize="28dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate Clicks"
    android:id="@+id/btnClickCalc"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textColor="#ff0000"
    android:onClick="calcClick" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/turretClicks"
    android:textSize="28dp"
    android:textColor="#ff0000"
    android:hint="0"
    android:background="#ffffff"
    android:layout_below="@+id/btnClickCalc"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="74dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/rangeEntry"
    android:layout_marginTop="91dp"
    android:textSize="20dp"
    android:hint="Enter Range"
    android:background="#ffffff"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clicks"
    android:id="@+id/textView2"
    android:layout_below="@+id/turretClicks"
    android:layout_centerHorizontal="true"
    android:background="#ffffff"
    android:textColor="#fc0000"
    android:textSize="20dp" />

The solutions below have fixed the original error, but I am now getting:

10-31 22:11:04.754 10019-10019/lol2dubs.stevemoa E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{lol2dubs.stevemoa/lol2dubs.stevemoa.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
lol2dubs
  • 67
  • 1
  • 8
  • Also, the onCreate method tells me too complex to analyze data flow algorithm. Should I pull all those if statements out and put them in an onClick or something? – lol2dubs Nov 01 '15 at 05:04

3 Answers3

5

Move your Textview definition inside onCreate() after setContentView().

Like this,

turretClicks = (TextView)findViewById(R.id.turretClicks);

Only after setting the ContentView, your Activity knows which layout file tree to parse to find the views you specified.

Next, make a cast of stringRange to an Integer inside onClick event and move all code after the cast inside onClick too to avoid NumberFormatException,

Also, add@Override annotation just before onClick method.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Shubham A.
  • 2,446
  • 4
  • 36
  • 68
  • Wish I could mark two answers. Your answer is correct, but Yuva Raj answered my second question, so I'm giving him the answer. – lol2dubs Nov 01 '15 at 16:16
  • You can mark any question as accepted. Glad to help. Though I don't see how Yuva answered your second question as you yourself answered it and he nowhere suggested to put your code inside `onClick`. – Shubham A. Nov 02 '15 at 04:16
0

Change your code to like this,

TextView turretClicks; // changed here
boolean noMOA;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    turretClicks = (TextView)findViewById(R.id.turretClicks); // added the line here

    ---- and so on --
Yuva Raj
  • 3,881
  • 1
  • 19
  • 30
  • Both your solutions cleared the error. But now I'm getting invalid int: 10-31 22:11:04.754 10019-10019/lol2dubs.stevemoa E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{lol2dubs.stevemoa/lol2dubs.stevemoa.MainActivity}: java.lang.NumberFormatException: Invalid int: "" – lol2dubs Nov 01 '15 at 05:13
  • Can you update your question with current log? The above doesn't show which line. There is a thread regarding Number Format Exception http://stackoverflow.com/questions/22502658/java-lang-numberformatexception-invalid-int-exception – Yuva Raj Nov 01 '15 at 05:17
  • I got it now. It was because all the if statements weren't in onClick. They were in onCreate. So the invalid int was because there was no entry in the range field when the activity was created. – lol2dubs Nov 01 '15 at 05:20
  • Nice. Accept the answer if it helps. Thanks :) – Yuva Raj Nov 01 '15 at 05:24
0

You must define the view after setContentView(R.layout.activity_main);

Copy that to your project:

public class MainActivity extends AppCompatActivity {
double MOA;
TextView turretClicks;
boolean noMOA;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

turretClicks = (TextView)findViewById(R.id.turretClicks);

EditText range = (EditText) findViewById(R.id.rangeEntry);
String stringRange = range.getText().toString();
int finalRange = Integer.parseInt(stringRange);
if(finalRange <= 200) {
    MOA = 0;
}
if(finalRange > 200 && finalRange <= 225){
    MOA = .5;
}
if(finalRange > 225 && finalRange <= 250) {
    MOA = 1;
}
if(finalRange > 250 && finalRange <= 275) {
    MOA = 1.65;
}
if(finalRange > 275 && finalRange <= 300) {
    MOA = 2.25;
}
if(finalRange > 300 && finalRange <= 325) {
    MOA = 2.8;
}
if(finalRange > 325 && finalRange <= 350) {
    MOA = 3.5;
}
if(finalRange > 350 && finalRange <= 375) {
    MOA = 4.0;
}
if(finalRange > 375 && finalRange <= 400) {
    MOA = 4.75;
}
if(finalRange > 400 && finalRange <= 425) {
    MOA = 5.50;
}
if(finalRange > 425 && finalRange <= 450) {
    MOA = 6.25;
}
if(finalRange > 450 && finalRange <= 475) {
    MOA = 7.0;
}
if(finalRange > 475 && finalRange <= 500) {
    MOA = 7.5;
}
if(finalRange > 500 && finalRange <= 525) {
    MOA = 8.25;
}
if(finalRange > 525 && finalRange <= 550) {
    MOA = 9.0;
}
if(finalRange > 550 && finalRange <= 575) {
    MOA = 9.75;
}
if(finalRange > 575 && finalRange <= 600) {
    MOA = 10.5;
}
if(finalRange > 600 && finalRange <= 625) {
    MOA = 11.5;
}
if(finalRange > 625 && finalRange <= 650) {
    MOA = 12.25;
}
if(finalRange > 650 && finalRange <= 675) {
    MOA = 13;
}
if(finalRange > 675 && finalRange <= 700) {
    MOA = 14;
}
if(finalRange > 700) {
   noMOA = true;
}


// Create an anonymous implementation of OnClickListener
View.OnClickListener btnClickCalc = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        double clicks = (MOA * 4);
        String toText = Double.toString(clicks);
        turretClicks.setText(toText);



    }
};

    // Capture our button from layout
    Button button = (Button)findViewById(R.id.btnClickCalc);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(btnClickCalc);

}
}
Mohamed
  • 656
  • 9
  • 28