So I have a button in my main activity class, populateButton, that calls a method in another class (.java file) and passes an array. This main class file also has a method to add some data to a TextView called addNumber:
public class CheckerActivity extends Activity {
public TextView displayArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checker);UMBERS_ARRAY);
displayArray = (TextView) findViewById(R.id.currentNumbers);
}
public void populateButton(View view) {
try {
String[] arrayOfLatestWinner = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
variousMethods giveArrayOfLatestWinners = new variousMethods();
String[] tester = giveArrayOfLatestWinners.checkNumbers(CheckerActivity.this, arrayOfLatestWinner);
} catch (Exception e) {
e.printStackTrace();
}
}
public void addNumber(String[] numbersToAdd) {
displayArray.setText(Arrays.toString(numbersToAdd));
}
I also have another class (.java file) with a method called checkNumbers that is called from the above populateButton.
public class variousMethods {
public String[] checkNumbers(Context context, String[] latestNumbersArray) {
try {
File myNumbersFile = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + context.getString(R.string.my_lotto_numbers_file))).toString());
BufferedReader myNumbersFileReader = new BufferedReader(new FileReader(myNumbersFile));
String inputLineOfMyNumbersFile;
inputLineOfMyNumbersFile = myNumbersFileReader.readLine();
while (inputLineOfMyNumbersFile != null) {
String[] arrayOfInputLine = inputLineOfMyNumbersFile.split(" ");
// Bad Code
CheckerActivity addNumbers = new CheckerActivity();
addNumbers.addNumber(arrayOfInputLine);
//SORRY, MISSED THIS LINE
inputLineOfMyNumbersFile = myNumbersFileReader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
When populatButton is pushed it sends String[] arrayOfLatestWinner to checkNumbers in variousMethods. That's fine. A file is opened and, what I want to happen, is that one line of the file is returned at a time and display it in the TextView via addNumbers(String[] numbersToAdd). (I relise in this code only the last line will end up being displayed eventualy it will populate a GridView) But the call to addNumber gives a NullPointerException.
Where am I going wrong please?
logcat is:
07-09 18:18:54.310 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ java.lang.NullPointerException
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.addNumber(CheckerActivity.java:84)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.variousMethods.checkNumbers(variousMethods.java:55)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:51)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.variousMethods.checkNumbers(variousMethods.java:93)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:51)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-09 18:18:54.320 29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)`