-2

I have 4 classes with the same code, there is only a difference, every class has a different file .txt path and I copyed and pasted the class just changing things like class name etc and obvliously the file path but it doesn't work.

The only path working is the same of the class where I copyed the code to paste it to the others classes, if I put the first path in every class it works, if I put differents path for every class the code working will be only the path in the class where I wrote the code manually, so I think that it could be a copy and paste bug of Android Studio. What can happen if I use copy and paste? I am in wrong or is it a bug?

The error is : java.lang.NullPointerException and this happen if I put a path that isn't the same of the class I wrote manually (obviously the files exists).

My code :

public class MyMain extends ListActivity {
    private String[] menus = new String[12];

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        String name = menus[position];
        try {
            Class main = Class.forName("com.Package."+name);
            Intent intent = new Intent(MyMain.this,main);
            startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void ReadPrices() throws IOException,MalformedURLException {
        // BufferedReader reader = new BufferedReader(new FileReader("/mnt/sdcard/Gaming_Prices.txt"));
        URL url = new URL("http://domain/myfile.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader((url).openStream()));
        String line = reader.readLine();
        int k = 0;
        for(k = 0; k<12; k++) {
            menus[k] = line;
            line=reader.readLine();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        try {
            ReadPrices();
            setListAdapter(new ArrayAdapter<String> (MyMain.this,android.R.layout.simple_expandable_list_item_1,menus));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

Error log :

12-24 02:24:48.369  25855-25855/sparkyka.it.pcbuilds E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
    at android.widget.AbsListView.obtainView(AbsListView.java:1609)
    at android.widget.ListView.makeAndAddView(ListView.java:1772)
    at android.widget.ListView.fillDown(ListView.java:695)
    at android.widget.ListView.fillFromTop(ListView.java:752)
    at android.widget.ListView.layoutChildren(ListView.java:1623)
    at android.widget.AbsListView.onLayout(AbsListView.java:1439)
    at android.view.View.layout(View.java:7175)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    at android.view.View.layout(View.java:7175)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    at android.view.View.layout(View.java:7175)
    at android.view.ViewRoot.performTraversals(ViewRoot.java:1144)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1863)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
    at dalvik.system.NativeStart.main(Native Method)
Zoe
  • 27,060
  • 21
  • 118
  • 148
ClearCode
  • 47
  • 4
  • 11
  • Another dupe of https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Zoe Feb 17 '19 at 20:10

2 Answers2

1

When copying this code to another class, are you changing this line to reference the new class, e.g. from MyMain.java to YourNewClass.java:

setListAdapter(new ArrayAdapter<String>  
 (MyMain.this,android.R.layout.simple_expandable_list_item_1,menus));
Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
tw1742
  • 1,424
  • 4
  • 18
  • 37
  • Sorry but I don't understand, I change the code for every class, every "MyMain" change in the name of the class, if I copy code in class "Test" every MyMain will change in Test – ClearCode Dec 24 '14 at 09:09
0

Are your classes Activities? If yes, did you declare them in Manifest?

EDIT: Check if everything is okay with this code:

Class main = Class.forName("com.Package." + name);

com.Package shouldn't contain uppercase letters. Use

Log.d("LOG", name[position]);

to see if everything okay with received name.

EDIT2: It seems like I see the problem. Your txt file is local file in folder assets, right? If no, create folder assets in root of your project and put there your txt file. Then:

AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

I hope this will help.

Yurets
  • 3,999
  • 17
  • 54
  • 74
  • My classes aren't Activities, I have a ListActivity and every item has a class, every class (item) has a different file to load. Clean Project doesn't fix the problem – ClearCode Dec 24 '14 at 01:22
  • @ClearCode, can you show log please and part of code? – Yurets Dec 24 '14 at 01:27
  • @ClearCode, please, need to see log and which line is null pointer? Also you may use Debug mode and toggle breakpoints where you think could be an issue. – Yurets Dec 24 '14 at 01:44
  • I think the error is relative the file because I put output to debug and I saw every message. Message edit with error – ClearCode Dec 24 '14 at 01:50
  • I obviously changed my personal data, my file are located online and package etc are working, everything is working but if I put this code in another class it doesn't work – ClearCode Dec 24 '14 at 02:11