0

I decided to optimize my code using a simple loop instead of hardcode, because previously I implemented each PickView separately and it worked fine.

Here is a problematic place:

pick = new PickView[9];
int id;
for(int i = 0; i < pick.length; i++) {
    id = getResources().getIdentifier("pick_" + i, "id", getPackageName());
    pick[i] = (PickView)findViewById(id);
    pick[i].setOnTouchListener(this); //this is MainActivity, line:54
}

...in which unexpectedly I recieved a NullPointerException:

ERROR/AndroidRuntime(8450): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.***}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.***.onCreate(MainActivity.java:54)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    ... 11 more

I enclose a PickView class:

public class PickView extends RelativeLayout {

    private TextView textView;
    private ImageView imageView;
    private String text;

    public PickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PickView);
        text = a.getString(R.styleable.PickView_text);

        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.pick_view, this);
        textView = (TextView)findViewById(R.id.pick_text);
        imageView = (ImageView)findViewById(R.id.pick_bg);
    }

    public void setText(String text) {
        textView.setText(text);
    }

    public void activate(boolean isActive) {
        if (isActive)
            imageView.setImageResource(R.drawable.pick_active);
        else
            imageView.setImageResource(R.drawable.pick);
    }
}

I will be grateful for any help!

3
  • (PickView)findViewById(id); return null ... why ? i'dont know ... show us more code ... maybe you call it before setContentView ... Commented Sep 6, 2012 at 13:22
  • You havn't initialized your array pick, try this first as in java, arrays must be initialized after declaration. Commented Sep 6, 2012 at 13:22
  • 1
    @engineer iteresting ... so what this line do: pick = new PickView[9]; ? Commented Sep 6, 2012 at 13:23

3 Answers 3

1

Just a small question do you have a pick_0 resource?

And anyway you should check that you are actually getting an id and that the PickView returned isn't null?

Sign up to request clarification or add additional context in comments.

1 Comment

You made my day! I had ids from "pick_1" to "pick_9". What a simple mistake... Thanks a lot! Stackoverflow rocks ;)
1

Java doc of getIdentifier suggests that it return 0 if not found that is the cause of your problem. So findViewById returns null if not found.

public int getIdentifier (String name, String defType, String defPackage)

Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)

1 Comment

Yes findViewById returns null if not found.
1

Maybe you can find it that way:

for (int i = 0; i < pick.length; i++)
{
  resources = getResources();
  if(resources == null)
  {
    System.out.println("oops.. no resources");
    throw new RuntimeException("oops... no resources");
  }
  id = resources.getIdentifier("pick_" + i, "id", getPackageName());
  pickView = (PickView) findViewById(id);
  if(resources == null)
  {
    System.out.println("oops.. noo pickView");
    throw new RuntimeException("oops... no pickView");
  }
  pick[i] = pickView;
  pick[i].setOnTouchListener(this); //this is MainActivity, line:54
}

1 Comment

It would be much, safer. ;) Next time I will write sth like this. Problem is solved by Raanan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.