简体   繁体   中英

read txt file into 2d array force exit

firstly i am sorry for my broken english. I wanna read txt file and into 2d array. this code is succesfully running on java. but android emulator gives warning message: "Sorry! the application has stopped unexpectedly. please try again"

how can I fix that? thanks

here is my code:

package com.example.hocam;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Konular extends ActionBarActivity {

  private ListView konuListe;
  private List<KonuBilgileri> konuBilgileri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_konular);
    Bundle dersadi = getIntent().getExtras();
    String dersinadi= dersadi.getString("Ders");

   switch(dersinadi)  {
   case "Turkce": 
       KonuGetir turkce=new KonuGetir("turkce.txt");

       String[][] turkcekonular=turkce.getKonular();

      System.out.println(turkcekonular[0][0]);  // it is the problem and give error
       konuListe = (ListView) findViewById(R.id.konu_listesi);
        konuBilgileri = new ArrayList<KonuBilgileri>();
       konuBilgileri.add(new KonuBilgileri(turkcekonular[0][0],R.drawable.turkce, turkcekonular[0][1]));  // array is problem

        MyAdapter adapter = new MyAdapter(Konular.this, R.layout.custom_list_item, konuBilgileri);
       konuListe.setAdapter(adapter);

       break;

   }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;

public class KonuGetir {
final int maxLines = 10100;
String[][] resultArray = new String[maxLines][];

public KonuGetir(String Ders){
      File file = new File(Ders);
        Scanner scanner;
        try {
            scanner = new Scanner(file,"utf-8");
             int linesCounter = 0;
                while (scanner.hasNextLine() && linesCounter < maxLines) {
                    resultArray[linesCounter] = scanner.nextLine().split("@");
                    linesCounter++;
                }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}
public String[][] getKonular()  {
    return resultArray;
}
}

logfile:

> 09-04 16:18:22.262: E/AndroidRuntime(1164): FATAL EXCEPTION: main
09-04 16:18:22.262: E/AndroidRuntime(1164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.os.Looper.loop(Looper.java:123)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at dalvik.system.NativeStart.main(Native Method)
09-04 16:18:22.262: E/AndroidRuntime(1164): Caused by: java.lang.NullPointerException
09-04 16:18:22.262: E/AndroidRuntime(1164):     at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:18:22.262: E/AndroidRuntime(1164):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:18:22.262: E/AndroidRuntime(1164):     ... 11 more
09-04 16:42:10.422: E/AndroidRuntime(1178): FATAL EXCEPTION: main
09-04 16:42:10.422: E/AndroidRuntime(1178): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.os.Looper.loop(Looper.java:123)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at dalvik.system.NativeStart.main(Native Method)
09-04 16:42:10.422: E/AndroidRuntime(1178): Caused by: java.lang.NullPointerException
09-04 16:42:10.422: E/AndroidRuntime(1178):     at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:42:10.422: E/AndroidRuntime(1178):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:42:10.422: E/AndroidRuntime(1178):     ... 11 more
09-04 16:43:28.802: E/AndroidRuntime(1207): FATAL EXCEPTION: main
09-04 16:43:28.802: E/AndroidRuntime(1207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.os.Looper.loop(Looper.java:123)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at dalvik.system.NativeStart.main(Native Method)
09-04 16:43:28.802: E/AndroidRuntime(1207): Caused by: java.lang.NullPointerException
09-04 16:43:28.802: E/AndroidRuntime(1207):     at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:43:28.802: E/AndroidRuntime(1207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:43:28.802: E/AndroidRuntime(1207):     ... 11 more

android manifest file:

 <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".YGS" android:label="@string/title_activity_ygs" > </activity> <activity android:name=".Konular" android:label="@string/title_activity_konular" > </activity> <activity android:name=".Video" android:screenOrientation="landscape" android:label="@string/title_activity_video" > </activity> </application> 

You are using the following code:

  String[][] turkcekonular=turkce.getKonular();

  System.out.println(turkcekonular[0][0]); 

But get java.lang.NullPointerException .

The reason for this because getKonular() returns an array initalised to size 10100 , however scanner does not contain any lines so scanner.hasNextLine() is false.

This means that turkcekonular[0] is null, as it was never set. Which means that turkcekonular[0][0] is trying to access a null object, so throws a NullPointerException

You should check if the object is null before trying to access it..

 if(turkcekonular[0] != null)
      System.out.println(turkcekonular[0][0]); 

and else where that this issue occurs.

(and you should figure out why your file is not read. Is it named correctly and exists in the specified path?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM