Inaparent java.lang.NullPointerException

I'm developing an android app and it takes me java.lang.NullPointerExceptionand I don't know what can be the reason. In the following lines you will have the logcat and the method that causes this crash (line 151).

04-17 18:56:19.729: E/AndroidRuntime(31291): FATAL EXCEPTION: main
04-17 18:56:19.729: E/AndroidRuntime(31291): java.lang.NullPointerException
04-17 18:56:19.729: E/AndroidRuntime(31291):    at tomorrowapps.com.tweeet.Main.DrawViewUp(Main.java:151)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at tomorrowapps.com.tweeet.Main.onOptionsItemSelected(Main.java:340)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.app.Activity.onMenuItemSelected(Activity.java:2548)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.view.View$PerformClick.run(View.java:17359)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.os.Handler.handleCallback(Handler.java:725)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.os.Looper.loop(Looper.java:137)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at android.app.ActivityThread.main(ActivityThread.java:5259)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at java.lang.reflect.Method.invoke(Method.java:511)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-17 18:56:19.729: E/AndroidRuntime(31291):    at dalvik.system.NativeStart.main(Native Method)

And here the method that causes the exception (line 151)

private void DrawViewUp() {
    // TODO Auto-generated method stub
    if(adview.getVisibility()==View.VISIBLE){
        drawview.bringToFront();                   //This is line 151
        adview.setVisibility(View.INVISIBLE);
        stroke.setVisibility(View.VISIBLE);
        rojo.setVisibility(View.VISIBLE);
        verde.setVisibility(View.VISIBLE);
        azul.setVisibility(View.VISIBLE);
        negro.setVisibility(View.VISIBLE);
    } else if (adview.getVisibility()==View.INVISIBLE){
        et1.bringToFront();
        adview.setVisibility(View.VISIBLE);
        stroke.setVisibility(View.INVISIBLE);
        rojo.setVisibility(View.INVISIBLE);
        verde.setVisibility(View.INVISIBLE);
        azul.setVisibility(View.INVISIBLE);
        negro.setVisibility(View.INVISIBLE);
    }
}

Also I have a method with all the findViewById that is runned on the onCreate method:

private void findViewById() {
    // TODO Auto-generated method stub
    et1=(EditText)findViewById(R.id.et1);
    tv1=(TextView)findViewById(R.id.tv1);
    views=(FrameLayout)findViewById(R.id.capture);
    drawview=(DrawView)findViewById(R.id.drawview);
    adview=(AdView)findViewById(R.id.adView);
    stroke=(SeekBar)findViewById(R.id.stroke);
    rojo=(ImageView)findViewById(R.id.rojo);
    verde=(ImageView)findViewById(R.id.verde);
    azul=(ImageView)findViewById(R.id.azul);
    negro=(ImageView)findViewById(R.id.negro);
}

So anyone knows why crashes?

EDIT: I removed all my code, was innnecesary

Answers


The problem is that drawviewII is null. Plain and simple. The probable cause of this is that findViewById() returns null if it can not find something by that id.


The problem was that in the DrawView class, the second and third constructors don't have in the super() the other variables of the constructor as you can see in the following example:

public class DrawView extends View {

    public DrawView(Context context) {
    super( context);
    }

    public DrawView(Context context, AttributeSet attrs) {
    super( context, attrs ); //first problem
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
    super( context, attrs, defStyle ); //second problem
    }

}

Solved using:

findViewById() returns null for custom component in layout XML, not for other components

http://igoesolutions.com/blog/2011/06/20/custom-view-is-not-using-the-2-or-3-argument-view-constructors-xml-attributes-will-not-work/


Need Your Help

implementation of a linked list with bucket?

c data-structures linked-list bucket

I have a linked list which stores 1 string of text in each node and each node can point to next node (basically what a linked list does).

Heredoc syntax pushing values into arrays

php arrays heredoc

I get a syntax error when I use heredoc syntax to push values into array

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.