0

I can´t figure out why this Integer.parseInt makes an exception, its a NullpointerException..

try
{
    int numberOfPictures = Integer.parseInt(editTextNumberOfGames
                        .getText().toString()); 
    Toast toast =Toast.makeText(getBaseContext(), "ratt", Toast.LENGTH_SHORT); 
                toast.show();
} catch (Exception e)
{
    Toast toast =Toast.makeText(getBaseContext(), "fel", Toast.LENGTH_SHORT); 
                toast.show();
}
4

3 回答 3

2

Have you used a debugger to verify that editTextNumberOfGames is not null when you get to this code?

You can temporary break up the Integer.parseInt(editTextNumberOfGames.getText().toString()) statement to see exactly which part of the compound statement is causing the exception.

See http://developer.android.com/reference/java/lang/Integer.html

于 2012-12-13T18:21:42.373 回答
0

As ratchet freak pointed out in comments, i had not done this:

editTextNumberOfGames = (EditText)findViewById(R.id.editTextNrOfPictures);

Thanks for all help though.

于 2012-12-13T18:28:43.820 回答
-2

It throws an expecption because editTextNumberOfGames is null

initialize editTextNumberOfGames correctly, or if for whatever reason you cannot garuantee, that it is initialized then

int numberOfPictures;
if (editTextNumberOfGames != null) {
   numberOfPictures = Integer.parseInt(editTextNumberOfGames.getText());
}
于 2012-12-13T18:18:24.370 回答