The Tokenizer
The Tokenizer

Reputation: 1564

null pointer exception when trying to access class

i am trying to get the value from my methods and display it in another class as a toast (Just to make sure my methods work). I am getting a null pointer exception that i cant seem to figure out, i've tried multiple differnt things. I've even tried giving my Strings values of "This" and "That". Then my log cat tells me

04-05 21:17:29.633: E/AndroidRuntime(18838): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Testing}: android.content.res.Resources$NotFoundException: String resource ID #0x0

This is the class i want to use to run everything.

public class Testing extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String Testeroni = "This";
        String Testerhynocerous = "That";

        LoginTest test = new LoginTest();

        Toast.makeText(getApplicationContext(), (test.TestUsername(Testeroni)),
                Toast.LENGTH_SHORT).show();

        Toast.makeText(getApplicationContext(),
                (test.TestPassword(Testerhynocerous)), Toast.LENGTH_SHORT)
                .show();

    }
}

This is the class that i am calling to run methods from.

public class LoginTest {

    // 0 = Username length is less than 4 or greater than 15
    // 1 = Username character is not a letter/or digit
    // 9 = Everything is okay in username :)
    public int TestUsername(String username) {
        if (username.length() <= 4 || username.length() >= 15) {

            return 0;
        }

        for (int i = 0; i < username.length(); i++) {
            if (Character.isDigit(username.charAt(i))
                    || Character.isJavaLetter(username.charAt(i))) {
                System.out.println("");
                // Do Nothing
            } else

                return 1;
        }
        return 9;
    }



        // 2 = Passowrd length is less than 4 or greater than 15
        // 3 = Password character is not a digit
        // 8 = Everything is okay in password :)
        public int TestPassword(String password) {
            if (password.length() <= 4 || password.length() >= 15) {
                return 2;
            }

            for (int i = 0; i < password.length(); i++) {
                if (Character.isDigit(password.charAt(i))
                        || Character.isJavaLetter(password.charAt(i))) {
                    System.out.println("");
                    // Do Nothing
                } else
                    return 3;
            }
            return 8;
        }
    }

Upvotes: 1

Views: 196

Answers (2)

idiottiger
idiottiger

Reputation: 5177

these code:

Toast.makeText(getApplicationContext(), (test.TestUsername(Testeroni)),
            Toast.LENGTH_SHORT).show();

    Toast.makeText(getApplicationContext(),
            (test.TestPassword(Testerhynocerous)), Toast.LENGTH_SHORT)
            .show();

have some problem, test.TestUsername(Testeroni) and test.TestPassword(Testerhynocerous) the return type is int, so it think like the string res id, so if can change to this:

 Toast.makeText(getApplicationContext(), (""+test.TestUsername(Testeroni)),
            Toast.LENGTH_SHORT).show();

    Toast.makeText(getApplicationContext(),
            (""+test.TestPassword(Testerhynocerous)), Toast.LENGTH_SHORT)
            .show();

Upvotes: 1

gwvatieri
gwvatieri

Reputation: 5173

The problem is here:

Toast.makeText(getApplicationContext(), (test.TestUsername(Testeroni)),
            Toast.LENGTH_SHORT).show();

The TestUsername returns an int and it is interpreted as resource id which of course is not valid.

Change it to:

Toast.makeText(getApplicationContext(), String.valueOf((test.TestUsername(Testeroni))),
            Toast.LENGTH_SHORT).show();

Upvotes: 1

Related Questions