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)