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: ""