1

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)`
Psyonic
  • 195
  • 9

3 Answers3

2

CheckerActivity is a Actiivty class. Its not a normal java class. So you should never create an isntance of Actiivty class.

This

CheckerActivity addNumbers = new CheckerActivity();
addNumbers.addNumber(arrayOfInputLine);

is wrong leading to NullPointerException.

More infor read Raghav Sood's answer @

Can i Create the object of a activity in other class?

For what your doing do you really need another class?. You can create a method in the activity class and call it whenever you want.

Edit:

You can create a Utility class

    public class Utility
    { 
        public String doSomething()
        {
            return "did something";
        }

    }       

In Activity

  Utility ul = new Utility();
  String result = ul.doSomething(); 
  // update ui with result  
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I know it's wrong (hence the Bad Code comment) I've tried many different ways of calling addNumber and read heaps but I just can't figure it out. Please tell me what is wrong and how to fix it. – Psyonic Jul 09 '14 at 08:27
  • @Ash have a method in your Activity class and call that when you need. You don't need another class – Raghunandan Jul 09 '14 at 08:28
  • That's what I am doing isn't it? The reason I am using a method in the main activity class is because it has access to the TextView – Psyonic Jul 09 '14 at 08:33
  • Oh, I get your last comment now. I was trying to split up the code so that the activity class handles the GUI stuff and I have another class to handle the grunt work. Is it getter to keep everything in the activity class? – Psyonic Jul 09 '14 at 08:35
  • @Ash are you using this functionality else where? – Raghunandan Jul 09 '14 at 08:48
  • No, but I might in the future dev of this project. I'm new to java and Android programming, quite the noob, this is my first Android app – Psyonic Jul 09 '14 at 08:54
  • @Ash no problem in having a method in activity class – Raghunandan Jul 09 '14 at 08:54
  • Yeah, I can do it there, that's originally where I had it, but I would like to know how to call methods this way from other classes – Psyonic Jul 09 '14 at 08:57
  • @Ash You cannot so that. If you want yoou can create a Utility class that does the work. But you cannot instantiate a Activity class. In utility class do all the functionality and in Activity create an instance of utility class and call the method the returns the result and display the same in ui in activity – Raghunandan Jul 09 '14 at 08:59
  • @Ash posted a small sample. rest is upto yout to design – Raghunandan Jul 09 '14 at 09:03
  • Is Utility class special? That's why I created the variousMethods class to handle the grunt work – Psyonic Jul 09 '14 at 09:38
  • I can do that no worries. I want the other way around. I want `public class Utility` to call a method in Activity so that the method in Activity can manipulate the GUI (add text to a TextView for example) – Psyonic Jul 09 '14 at 09:44
  • @Ash as i said earlier that is not possible. – Raghunandan Jul 09 '14 at 09:44
  • Ohh, so how would one update something in the UI from a method in a different class without returning? – Psyonic Jul 09 '14 at 09:51
  • @Ash using a hndler a broadcast receiver. – Raghunandan Jul 09 '14 at 09:52
1

Return array here

public class variousMethods {

public void checkNumbers(Context context, TextView displayArray, 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(" ");

        inputLineOfMyNumbersFile = myNumbersFileReader.readLine();
        while (inputLineOfMyNumbersFile != null) {
            displayArray.setText(Arrays.toString(inputLineOfMyNumbersFile));
            inputLineOfMyNumbersFile = myNumbersFileReader.readLine();
        }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

and you can remove add number method.

variousMethods giveArrayOfLatestWinners = new variousMethods();
giveArrayOfLatestWinners.checkNumbers(CheckerActivity.this, displayArray, arrayOfLatestWinner);
sujithvm
  • 2,351
  • 3
  • 15
  • 16
  • Oopps, sorry, I missed a line of code there. Please see the update question where you say to put the return. What I want is to read a line of the file, put that line in a TextView, then read another line, put that in the same TextView etc. untill EOF (I know it will only show the last line of the file when finished, will populate a GridView eventually) – Psyonic Jul 09 '14 at 08:42
  • @Ash why dont you pass reference to your TextView as well to the method so that changes can be updated in the `variousMethods` function itself ? – sujithvm Jul 09 '14 at 08:44
  • That's exactly what I would like to do but Google won't tell me how! (I don't know what to ask it) That would be ideal! How does one do that? Is that what the folling answer is getting at? – Psyonic Jul 09 '14 at 08:48
  • @Ash i have updated answer. Not sure if this is really want you want – sujithvm Jul 09 '14 at 09:00
  • That seams what I want, and I had tried that before but it gives `cannot resolve symbol 'displayArray` I think because displayArray is defined in a different class?? – Psyonic Jul 09 '14 at 09:05
  • @Ash just check variable name `displayArray` is spelled correctly. You are passing textview `displayArray` as a parameter here. – sujithvm Jul 09 '14 at 09:07
  • Definitely spelt correctly. Seams to me like it needs something like `CheckerActivity.` in front of it but that gives `cannot be referenced from static context` error ??? Any thoughts? – Psyonic Jul 09 '14 at 09:36
  • @Ash which part of code are you getting this error? – sujithvm Jul 09 '14 at 09:38
  • I am using Android Studio so when I type it into the location you said, it gets flagged – Psyonic Jul 09 '14 at 09:47
  • @Ash if you can post exact code or pic it would be great. – sujithvm Jul 09 '14 at 09:52
  • Have an image, not sure how to share it. The docs say it must be a valid HTML link... – Psyonic Jul 09 '14 at 09:59
  • @Ash can you upload it or put in dropbox and share ? – sujithvm Jul 09 '14 at 10:01
  • Try https://dl.dropboxusercontent.com/u/106044397/Screenshot%20from%202014-07-09%2019%3A54%3A10.png – Psyonic Jul 09 '14 at 10:13
  • @Ash you havent updated the function prototype. I have added textview to the function see `public void checkNumbers(Context context, TextView displayArray, String[] latestNumbersArray) {` and call this by second code snippet i posted – sujithvm Jul 09 '14 at 10:15
  • That is sooooo cool, didn't know you could manipulate items IN the GUI directly from other class files!! Still getting an ArrayIndexOutOfBoundsException but I can deal with that easy!! Thank you soooo much! You ROCK!! – Psyonic Jul 09 '14 at 10:35
  • Thank you for sticking with me. You've been the most help I've experienced in this forum so far! No to say others have not been helpful but you take the cake!! Thanks mate! – Psyonic Jul 09 '14 at 10:42
0

you have to use singleton in this case,use getinstance method you can call method of activity like:

In Your activity

 static YourActivity _activity;

public static Activity getInstance(){ return _activity; }

In oncreate:

   _activity = this

Whereever you want to use any method of this activity just call like:

_activity.getInstance().callmethod();
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59