I get a nullpointerexception error but I don't know why

Hey guys. I have a simple question about my android app. I know what you are thinking " this dumbass just posted this in the wrong thread". But I did it for a reason. The error that I have is related to java...not android.

(I will star every line of code that I talk about)

So here is the problem: I get a NullPointerException at line 75 in the MainActivity class. So what I do is look at that line. It reads:

if(number.length() == 0){

So what I'm thinking is that I forgot to set the value of number. Then I look right above that and it says:

, and I find that I assigned the variable to phone_number, which if you are smart enough to figure out is a string with a phone number inside of it. At this point I don't know what to do. I made sure everything was public, but I'm sure it has to do with the placement of variables inside brackets but I am just not seeing it.

that is your declaration of phone_number in your activity class. now, you instantiate your activity class, but you never set a value to phone_number, so ... yes, it does still have a null-reference.

you may want to do a few things here:
- set default values, or define a constructor that does so. you haven't set a constructor yourself, so you'll use the one the compiler generates for you, and in that one, you don't set a value to phone_number.
- keep your instance variables private, having all your variables public is pretty bad design. set them to private and use mutators.

I created a settings menu for my app that requires a phone number
Then I made it so the user can choose a contact from a contact dialog.
The user will click a button that says "choose a contact"
Then the dialog will open and they will click on a name
Then the app will fetch the phone number and set the text of a edittext**** as the phone number in case they want to edit it or anything.
Then i set the variable 'number' from the MainActivity class to 'phone_number' from the SettingsActivity class(which is also the same variable used to set the text of the edittext****)

After I choose the contact, the number shows up in the edittext which tells me that phone_number has the right value

The problem is, is when I try to set 'number' in the mainactivity class to 'phone_number' from the SettingsActivity class.

well ... I didn't even know you had a startListener so .. that wasn't really what I was talking about.
what I meant was, in your Activity class (inherited members taken into account), you basically have:
(and I'm not using the right names here)

public class MyActivity{
public String phone_number;
public MyActivity(){}
}

In this part of the code, you don't set a value for phone_number. since it's an instance member, it's default set to null.

so, at first, you run the default constructor, which looks like the one I added above in that MyActivity example. it doesn't do anything, except for initializing the instance of SettingsActivity. that doesn't mean it initializes the instance variables.

change that Activity class to either:

public class MyActivity{
public String phone_number ="00";
public MyActivity(){}
}

or to

public class MyActivity{
public String phone_number;
public MyActivity(){this.phone_number="00";}
}

the settings.phone_number call will there return "00" instead of null, so at least the nullpointer 'll be solved.

my other point was: don't make your instance members public. you shouldn't be able to call them by: settings.phone_number. make use of the OO concepts, like encapsulation, to make your code a lot safer.so, my suggestion, it would be something like:

now, they're still bogus, but now the access and influence the calling classes have on the instance member(s) are more restricted, giving you more abilities to guarantee the contents or validation of those members.

either phoneNumber or message contains an invalid value (empty or null). I'll add a link to the api here: [sendTextMessage](http://developer.android.com/reference/android/telephony/SmsManager.html#sendTextMessage(java.lang.String, java.lang.String, java.lang.String, android.app.PendingIntent, android.app.PendingIntent)) just try this with hardcoded values, and check if it works then. if so, you'll need to debug to ensure you get the right values when running that line.

I am getting an error when I run my program that says:
Exception in thread "main" java.lang.NullPointerException
at defaultpackage.GradeBook.setName(GradeBook.java:25)
at defaultpackage.GradeBookDemo.readFromFile(GradeBookDemo.java:52)
at defaultpackage.GradeBookDemo.main(GradeBookDemo.java:23)
I've looked …

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...