2

I am writing an Android app where I need to pass a string array between two classes. The string initializes fine and I can output the contents of the string fine in the one class but as I try to pass it to another class I get a Null Pointer Exception error. The following is the stripped down version of my code:

accelerometer.java:

    public class accelerometer extends Service {

 public String movement[];

 public void onCreate() {

  movement = new String[1000000];

 }

 public void updatearray() {
  movement[arraypos]=getCurrentTimeString();
  //Toast.makeText(this, movement[arraypos] , Toast.LENGTH_SHORT).show(); //this correctly displays each position in the array every time it updates so I know the array is working correctly in this file
  arraypos+=1;
 }

 public String[] getmovement(){
  return movement;
 }
    }

wakeupalarm.java:

    public class wakeupalarm extends Activity {

 private TextView herestext_;

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.wakeup);

  herestext_ = (TextView) findViewById(R.id.TextView01);

  accelerometer accelerometercall = new accelerometer();

  String movearray[] = accelerometercall.getmovement();

  herestext_.setText(movearray[2]);
  }

    }

I have a feeling I'm missing something very simple but any help would be greatly appreciated!

Thanks,

Scott

2 Answers 2

1

You're creating a new accelerometer class, which is completely uninitialized since there is no constructor, then you access its member. Of course it'll be null.

Not sure how your two classes are related, but if the activity is called by the service, then you need to pass the string through the intent (through an extra, for example).

Side note: Class names should always start with a capital letter. Method/variable names should have camel case, i.e. "updateArray". Also, you can format your code here by selecting it and pressing CTRL+K.

Sign up to request clarification or add additional context in comments.

Comments

0

Your first problem, I think, is that you are creating an array with a million slots in it. Do you really mean to be doing that? It's going to take a lot of memory---quite possibly more than is available. You should instead look to having a Vector of Strings that you extend as necessary.

2 Comments

I doubt the million slot array is causing a NullPointerException to be thrown. If the OP was running out of memory you would see an OutOfMemoryException or something like that. Although, I do agree that it's probably an unnecessarily large array.
Yeah, I wasn't answering the question, so much as complaining about the programming style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.