0

i have an arraylist that do this :

     ArrayList<Integer> id = new ArrayList<Integer>(); 
     for (int i = 0; i <= 20; i++) {
     id.add(getResources().getIdentifier("q"+i, "raw", getPackageName()));}

this method before a little change is working good but now have force close! and i get this logcat:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{yaAli.package313.hossein110/yaAli.package313.hossein110.know}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)at android.app.ActivityThread.access$600(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at yaAli.package313.hossein110.know.onCreate(know.java:33)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
... 12 more

Here is my OnCreate():

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.basetxt);        
    SharedPreferences   settings=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    ln=settings.getString("LASTREADln", null);
    if(ln.equals("-1")){ln="0";}
    if(ln!=null){
    final ScrollView s1=(ScrollView) findViewById(R.id.sV1);
    s1.post(new Runnable() {@Override
    public void run() {s1.scrollTo(0, Integer.valueOf(ln));} });}

    final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.arza);  

    String pos = getIntent().getStringExtra("key");
    String arr = getIntent().getStringExtra("list");


    TextView tvfa = (TextView)findViewById(R.id.TEXT313);

    String fontPath = "fonts/font1.ttf";
    String fontPath1 = "fonts/font2.ttf";

    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
    Typeface tf1 = Typeface.createFromAsset(getAssets(), fontPath1);

    SharedPreferences sharedpreferences =    PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    tvfa.getRootView().setKeepScreenOn(sharedpreferences.getBoolean("scrnon", false));
    String sizefa=   sharedpreferences.getString("fontsizefa",null);
    String colorfa=   sharedpreferences.getString("fontcolorfa",null);        
    boolean style=   sharedpreferences.getBoolean("appfont", false);
    boolean music=   sharedpreferences.getBoolean("musictype", false);
    boolean curr=   sharedpreferences.getBoolean("currputfont", false);

String t = read(file(pos,arr,null)); {

if (curr){tvfa.setText(PersianReshape.reshape(t));}else{tvfa.setText(t);} // Txt 
tvfa.setTextSize(1, Float.valueOf(sizefa).floatValue());                    // Size
tvfa.setTextColor(Color.parseColor(colorfa));                               // Color
if (style) { tvfa.setTypeface(tf1); }  else {tvfa.setTypeface(tf);}         // Type
if (music) { mp1.start(); } else { mp1.stop(); }  }}                          // Play
//---------------------------------------------------------------------------- 
Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
hkh114
  • 162
  • 1
  • 3
  • 15
  • 3
    obvious : what happens at `yaAli.package313.hossein110.know.onCreate(know.java:33)` ? – njzk2 Oct 05 '12 at 15:53
  • Also - if you're using the recommended Eclipse environment (the only one I've tried), then debugging is staggeringly easy. Have you breakpointed that line, and stepped through all the functions to see where the null is creeping in? – cloudfeet Oct 05 '12 at 15:55
  • You have a null-pointer in your onCreate() method. – Shark Oct 05 '12 at 16:02
  • Since we can't tell what line 33 of your onCreate() is, you have to help out. My guess is that your content view has changed, so when you did a findViewById() it came back null. Later on when you call post(), things may fail. THis is a guess though only because there is not enough info. – Greg Giacovelli Oct 05 '12 at 16:11

2 Answers2

2

best practice for java development is to have the literal string do the .equals call. so instead of:

var.equals("string")

you do:

"string".equals(var)

This guarantees you will NEVER have a null pointer exception when doing string comparison.

Also, it looks like you are storing numeric values as strings. Any particular reason you aren't storing them as ints?

toadzky
  • 3,806
  • 1
  • 15
  • 26
1

Its likely here

ln=settings.getString("LASTREADln", null);

this should be

ln=settings.getString("LASTREADln", "");

since null is set to be your default value if that key does not exist or contain anything, so if it doesn't contain anything you should set it to "", and for your string comparisons you should look for !ln.contentsEquals("") instead of checking it for null

the same goes for all of the strings you get from a preferences file. set the default value to "" instead of null

CQM
  • 42,592
  • 75
  • 224
  • 366
  • if this helped you please vote it upward, if it solved your main problem please mark it as correct also – CQM Oct 05 '12 at 17:21