-1

В моем проекте я получаю nullpointerexception в классе, показанном ниже, также я прикрепил logcat и java-класс ниже. В случае if я дал условие типа «if (name == null)», я не уверен, что это правильно или неправильно Если неправильные средства предложить мне правильный код для решения NPE ..Исключительное исключение указателя в проекте андроида

Спасибо заранее

public void onClick(DialogInterface dialog,int id) 
{ 
         // yes(); 
Intent tablewise = new Intent(Seating_Engagement.this,LoginForm.class); 
          startActivity(tablewise); 
          finish(); 

         } 
       }) 

LogCat:

10-13 10:25:33.914 24201-24201/com.sentientit.theiWedplanner E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.NullPointerException 
      at com.sentientit.theiWedplanner.Seating_Engagement$2.onClick(Seating_Engagement.java:77) 
      at android.view.View.performClick(View.java:4421) 
      at android.view.View$PerformClick.run(View.java:18190) 
      at android.os.Handler.handleCallback(Handler.java:725) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:175) 
      at android.app.ActivityThread.main(ActivityThread.java:5279) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
      at dalvik.system.NativeStart.main(Native Method) 
10-13 10:25:45.445 24201-24206/com.sentientit.theiWedplanner I/dalvikvm﹕ threadid=3: reacting to signal 3 
10-13 10:25:45.578 24201-24206/com.sentientit.theiWedplanner I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt' 

в строке 77 исключение происходит я ознаменовал линии, как (77) в файле Java ниже

Seating_Engagement

import android.app.AlertDialog; 
    import android.app.Dialog; 
    import android.app.ProgressDialog; 
    import android.content.Context; 
    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.net.ConnectivityManager; 
    import android.net.NetworkInfo; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 
    import android.view.View; 
    import android.widget.Button; 

    import com.bugsense.trace.BugSenseHandler; 

    import java.util.StringTokenizer; 

    public class Seating_Engagement extends FragmentActivity{ 

     ProgressDialog myProgressDialog = null; 
     String username=welcomeuser1.usernamevalue; 
     String result=null; 
     String nameresult=null; 
     String Table,Chart=null; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);  BugSenseHandler.initAndStartSession(this, "68640bea"); 

      setContentView(R.layout.seatingchart); 

      Button familywise=(Button)findViewById(R.id.familywise); 
      Button tablewisesummary=(Button)findViewById(R.id.tableclassify); 
      Button assignedguest=(Button)findViewById(R.id.assignedguest); 
      Button notassignedguest=(Button)findViewById(R.id.notassignedguest); 
      Button tablechair=(Button)findViewById(R.id.tableclass); 
      Button groupwise=(Button)findViewById(R.id.groupwise); 
      Button tablewise=(Button)findViewById(R.id.tablewisesummary); 
      Button familywisesummary=(Button)findViewById(R.id.familywisesummary); 
      Button groupwisesummary=(Button)findViewById(R.id.groupwisesummary); 
      Button arrangementsummary=(Button)findViewById(R.id.totaltables); 


      Button back=(Button)findViewById(R.id.back); 
      Button seatingoverview =(Button)findViewById(R.id.seatingview); 

      back.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        finish(); 
       } 
      }); 

      seatingoverview.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        if(username.equals("null")) 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 
         .setTitle("Message!") 
         .setPositiveButton(R.string.ok1, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
         int id) { 
         // yes(); 
(77) Intent tablewise = new Intent(Seating_Engagement.this,LoginForm.class); 
          startActivity(tablewise); 
          finish(); 
          } 
        }) 
         .setMessage("Registered users can access this page. Click 'Yes' to login") 
         .setNegativeButton(R.string.no, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 


          } 
         }) 

         .create(); 
         locationError.show(); 
        } 
        else 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 

         .setPositiveButton("Table", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             Seating_EngagementTable.class); 
           startActivity(tablewise); 
           // finish(); 


          } 
         }) 

         .setNegativeButton("Chart", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           Intent tab=new Intent(Seating_Engagement.this,Seating_Engagementviewtable.class); 
           startActivity(tab); 
           finish(); 


          } 
         }) 

         .create(); 
         locationError.show(); 


        } 



       } 
      }); 


      notassignedguest.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        Intent tablewise=new Intent(Seating_Engagement.this,Engagement_Notyetseated.class); 
        startActivity(tablewise); 
        //     finish(); 

       } 

      }); 

      assignedguest.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Intent tablewise=new Intent(Seating_Engagement.this,Engagement_AssignedGuest.class); 
        startActivity(tablewise); 
        //finish(); 

       } 

      }); 



      tablechair.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Intent tablewise=new Intent(Seating_Engagement.this,Engagement_TableClassify.class);  
        startActivity(tablewise); 
        //     finish(); 

       } 
      }); 


      tablewisesummary.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        if(username.equals("null")) 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 
         .setTitle("Message!") 
         .setPositiveButton(R.string.ok1, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             LoginForm.class); 
           startActivity(tablewise); 
           finish(); 


          } 
         }) 
         .setMessage("Registered users can access this page. Click 'Yes' to login") 
         .setNegativeButton(R.string.no, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 


          } 
         }) 

         .create(); 
         locationError.show(); 
        } 
        else 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 

         .setPositiveButton("Table", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             Engagement_TableChairSummary.class); 
           startActivity(tablewise); 
           //finish(); 


          } 
         }) 

         .setNegativeButton("Chart", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           Intent tab=new Intent(Seating_Engagement.this,Engagement_ViewTable.class); 
           startActivity(tab); 
           finish(); 


          } 
         }) 

         .create(); 
         locationError.show(); 


        } 



       } 
      }); 


      familywisesummary.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        if(username.equals("null")) 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 
         .setTitle("Message!") 
         .setPositiveButton(R.string.ok1, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             LoginForm.class); 
           startActivity(tablewise); 
           finish(); 


          } 
         }) 
         .setMessage("Registered users can access this page. Click 'Yes' to login") 
         .setNegativeButton(R.string.no, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 


          } 
         }) 

         .create(); 
         locationError.show(); 
        } 
        else 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 

         .setPositiveButton("Table", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             Engagement_Familywisesummary.class); 
           startActivity(tablewise); 
           //finish(); 


          } 
         }) 

         .setNegativeButton("Chart", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           Intent tab=new Intent(Seating_Engagement.this,Engagement_Familyviewtable.class); 
           startActivity(tab); 
           finish(); 


          } 
         }) 

         .create(); 
         locationError.show(); 

        } 


       } 
      }); 

      groupwisesummary.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        if(username.equals("null")) 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 
         .setTitle("Message!") 
         .setPositiveButton(R.string.ok1, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             LoginForm.class); 
           startActivity(tablewise); 
           finish(); 


          } 
         }) 
         .setMessage("Registered users can access this page. Click 'Yes' to login") 
         .setNegativeButton(R.string.no, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 


          } 
         }) 

         .create(); 
         locationError.show(); 
        } 
        else 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 

         .setPositiveButton("Table", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             Engagement_Groupwisesummary.class); 
           startActivity(tablewise); 
           //finish(); 


          } 
         }) 

         .setNegativeButton("Chart", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           Intent tab=new Intent(Seating_Engagement.this,Engagement_Groupviewtable.class); 
           startActivity(tab); 
           finish(); 


          } 
         }) 

         .create(); 
         locationError.show(); 

        } 


       } 
      }); 

      familywise.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 


        // String user_url ="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/tabledisplay.aspx?unme="+username+"&occasion=Engagement"; 
        String user_url ="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/version21/seating-table-display.aspx?unme="+username+"&occasion=Engagement"; 


        httpclass obj = new httpclass(); 
        result = obj.server_conn(user_url); 

        StringTokenizer st = new StringTokenizer(result, "|"); 
        result = st.nextToken(); 

        // String user ="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/Notallocated_familywise.aspx?unme="+username+"&occasion=Engagement"; 

        String user="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/version21/notallocated_familywise.aspx?unme="+username+"&occasion=Engagement"; 

        httpclass objg = new httpclass(); 
        nameresult = objg.server_conn(user); 

        StringTokenizer s = new StringTokenizer(nameresult, "|"); 
        nameresult = s.nextToken(); 

        if(result.equals("There is no table")) 
        { 

         alertbox("Assignment", "None of the table has been created"); 

        } 
        else if(nameresult.equals("No Guests")) 
        { 

         alertbox("Assignment", "No Guest invited"); 

        } 
        else 
        { 
         boolean netvalue = false; 
         ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 


         NetworkInfo info = cm.getActiveNetworkInfo(); 
         if (info != null && info.isAvailable()) { 
          netvalue = true; 
          Intent tablewise=new Intent(Seating_Engagement.this,Engagement_FamilwiseAllocation.class); 

          startActivity(tablewise); 
          // finish(); 
         } 
         else 
         { 
          alertbox("Message!", "No Internet Connection!"); 
         } 
        } 




       } 
      }); 

      groupwise.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 


        // String user_url ="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/tabledisplay.aspx?unme="+username+"&occasion=Engagement"; 

        String user_url ="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/version21/tabledisplay.aspx?unme="+username+"&occasion=Engagement"; 

        httpclass obj = new httpclass(); 
        result = obj.server_conn(user_url); 

        StringTokenizer st = new StringTokenizer(result, "|"); 
        result = st.nextToken(); 

        // String user ="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/Not_allocated_notfamily.aspx?unme="+username+"&occasion=Engagement"; 
        String user="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/version21/Not_allocated_notfamily.aspx?unme="+username+"&occasion=Engagement"; 


        httpclass objg = new httpclass(); 
        nameresult = objg.server_conn(user); 

        StringTokenizer s = new StringTokenizer(nameresult, "|"); 
        nameresult = s.nextToken();   
        if(result.equals("There is no table")) 
        { 

         alertbox("Summary", "None of the table has been created"); 

        } 
        else if(nameresult.equals("No Guests")) 
        { 

         alertbox("Assignment", "No Guest invited"); 

        } 
        else 
        { 

         Intent tablewise=new Intent(Seating_Engagement.this,Engagement_GroupWiseAllocation.class);        
         startActivity(tablewise); 
         //finish(); 
        } 


       } 
      }); 

      tablewise.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        if(username.equals("null")) 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 
         .setTitle("Message!") 
         .setPositiveButton(R.string.ok1, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             LoginForm.class); 
           startActivity(tablewise); 
           finish(); 


          } 
         }) 
         .setMessage("Registered users can access this page. Click 'Yes' to login") 
         .setNegativeButton(R.string.no, 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 


          } 
         }) 

         .create(); 
         locationError.show(); 
        } 
        else 
        { 
         Dialog locationError = new AlertDialog.Builder(
           Seating_Engagement.this) 
         .setIcon(0) 

         .setPositiveButton("Table", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // yes(); 
           Intent tablewise = new Intent(Seating_Engagement.this, 
             Engagement_TableWiseSummary.class); 
           startActivity(tablewise); 
           //finish(); 


          } 
         }) 

         .setNegativeButton("Chart", 
           new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           Intent tab=new Intent(Seating_Engagement.this,Engagement_Tableviewtable.class); 
           startActivity(tab); 
           finish(); 


          } 
         }) 

         .create(); 
         locationError.show(); 
        } 



       } 
      }); 



      arrangementsummary.setOnClickListener(new View.OnClickListener() { 

       //@Override 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        // String user_url ="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/tabledisplay.aspx?unme="+username+"&occasion=Engagement"; 

        String user_url ="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/version21/tabledisplay.aspx?unme="+username+"&occasion=Engagement"; 


        httpclass obj = new httpclass(); 
        result = obj.server_conn(user_url); 

        StringTokenizer st = new StringTokenizer(result, "|"); 
        result = st.nextToken(); 


        if(result.equals("There is no table")) 
        { 

         alertbox("Summary", "None of the table has been created"); 

        } 
        else 
        { 

         Intent tablewise=new Intent(Seating_Engagement.this,Engagement_TotalSeatingStatus.class);   
         startActivity(tablewise); 
         // finish(); 
        } 
       } 

      }); 

     } 
     protected void alertbox(String title, String mymessage) 
     { 
      new AlertDialog.Builder(this) 
      .setMessage(mymessage) 
      .setTitle(title) 
      .setCancelable(true) 
      .setNeutralButton(android.R.string.ok, 
        new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int whichButton){} 
      }) 
      .show(); 
     } 
     @Override 
     public void onStart() { 
      super.onStart(); 
      // The rest of your onStart() code. 
      // EasyTracker.getInstance(this).activityStart(this); // Add this method. 
     } 

     @Override 
     public void onStop() { 
      super.onStop(); 
      // The rest of your onStop() code. 
      // EasyTracker.getInstance(this).activityStop(this); // Add this method. 
     } 

    } 
+4

вместо 'username.equals ("нулевой")' 'использовать имя пользователя == null' –

+0

@shayan pourvatan все еще получаю такую ​​же NPE, изменив имя пользователя == NULL –

ответ

0

Вы не проверяя имя пользователя для нуля должным образом. Вот где ваш NPE лежит, имя пользователя равно null, но вы вызываете на нем метод equals().

Изменить это:

if(username == null){ 
    (...) 
} 
+0

@ jvrodrigues Я изменил его (имя пользователя = = null), все еще получая тот же NPE. –

0

попробовать это ...

if (TextUtils.isEmpty(username)) { 

     //code here 

    } else{ 

     //code here 
    } 

или

if(username.equals("")){ 

    Toast.makeText(getApplicationContext(), "Please Enter Username", Toast.LENGTH_SHORT).show(); 
} 
else{ 

    //code here  

} 
+0

Я попробовал все еще получать NPE.if, если есть какой-либо другой способ его решить –

+0

Вы пробовали вышеупомянутый @Sanju ?? – Naveen

+0

да я постарался все еще получать то же самое NPE –

0

Вы можете изменить (77) Намерение tablewise = новый Намерение (Seating_Engagement.this , LoginForm.class);

к

Намерение tablewise = новый Intent (getActivity(), LoginForm.class);

getActivity(). StartActivity (tablewise);

+0

Что касается «getActivity()», он показывает ошибку на нем, когда я пробовал этот код –

+0

http://stackoverflow.com/questions/12610995/what-does-getactivity-mean –

+0

Если вы хотите, чтобы Sit_Engagement был фрагментом, вы можете change public class Seating_Engagement extends Фрагмент –

0
final String user= username.getText().toString(); 

if (user.matches("")) { 
    username.setError("Please Enter Name"); 
} 
Смежные вопросы