什么是错的>?我不能做

时间:2016-10-22 07:38:36

标签: java android database nullpointerexception callback

但有些不对劲,我是新的。 我的代码

public class MainActivity extends AppCompatActivity {
    //kluci dla soedenenia
    public static final String APPI_ID = "----------------------";
    public static final String SECRET_KEY = "----------------";
    public static final String VERSION = "---";
    //moy log
    private static final String TAG = "myLogs";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //soedenil backendless
        Backendless.initApp(this, APPI_ID, SECRET_KEY, VERSION);

        Customer melinda = new Customer();
        melinda.setFirstName( "Melinda" );
        melinda.setLastName( "Grey" );
       Backendless.Persistence.of(Customer.class).save(melinda, new AsyncCallback<Customer>() {
           @Override
           public void handleResponse(Customer response) {

           }

           @Override
           public void handleFault(BackendlessFault fault) {

           }
       });
        System.out.println("Melinda's rate is "+melinda.getDiscountRatePercent()+"percent");
                melinda.setDiscountRatePercent( melinda.getDiscountRatePercent() + 2 );
        Backendless.Persistence.of(Customer.class).save(melinda, new AsyncCallback<Customer>() {
            @Override
            public void handleResponse(Customer customer) {

            }

            @Override
            public void handleFault(BackendlessFault fault) {

            }
        });
        Customer updatedMelinda = new Customer();
        updatedMelinda.setFirstName( "Melinda" );
        updatedMelinda.setLastName( "Grey" );

        updatedMelinda = Backendless.Persistence.of( Customer.class ).findById
                ( melinda.getID() );
        System.out.println("Now Melinda's discount rate is "+updatedMelinda.
                getDiscountRatePercent() +" percent.");

    }
        }

客户类

public class Customer {
    private String lastName;
    private Integer discountRatePercent;
    private String firstName;
    public String getLastName()
    {
        return lastName;
    }
    public void setLastName( String lastName )
    {
        this.lastName = lastName;
    }
    public Integer getDiscountRatePercent()
    {
        return discountRatePercent;
    }
    public void setDiscountRatePercent( Integer discountRatePercent )
    {
        this.discountRatePercent = discountRatePercent;
    }
    public String getFirstName()
    {
        return firstName;
    }
    public void setFirstName( String firstName )
    {
        this.firstName = firstName;
    }
}

我的java错误

  

10-22 03:14:23.620 2774-2774 /? E / AndroidRuntime:致命异常:主要                                                    处理:com.example.elvir.subdexample,PID:2774                                                    java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.elvir.subdexample / com.example.elvir.subdexample.MainActivity}:java.lang.NullPointerException                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)                                                        在android.app.ActivityThread.access $ 800(ActivityThread.java:135)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:136)                                                        在android.app.ActivityThread.main(ActivityThread.java:5017)                                                        at java.lang.reflect.Method.invokeNative(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:515)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                        at dalvik.system.NativeStart.main(Native Method)                                                     引起:java.lang.NullPointerException                                                        at com.example.elvir.subdexample.MainActivity.onCreate(MainActivity.java:40)                                                        在android.app.Activity.performCreate(Activity.java:5231)                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)                                                        在android.app.ActivityThread.access $ 800(ActivityThread.java:135)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:136)                                                        在android.app.ActivityThread.main(ActivityThread.java:5017)                                                        at java.lang.reflect.Method.invokeNative(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:515)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                        在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:1)

尝试替换

updatedMelinda = Backendless.Persistence.of( Customer.class ).findById
            ( updatedMelinda );

通过

updatedMelinda = Backendless.Persistence.of( Customer.class ).findById
            ( melinda.getID() );