尝试使用Singleton访问变量时的NPE

时间:2013-02-21 04:07:10

标签: android nullpointerexception singleton

我正在为一个大学项目工作,我正在使用Singleton设计模式从我的应用程序中的任何活动全局访问变量。

运行我的应用程序时出现此错误:

java.lang.NullPointerException at com.example.waitronproto9.SectionsActivity$2.onClick(SectionsActivity.java:80)

这是与之相关的代码行:

Order.getInstance().setTableNumber(table);

这是我的整个SectionsActivity

public class SectionsActivity extends Activity{

    LayoutInflater inflater;
    ActionBar ab;
    Button homeBtn;
    Button viewBtn;
    Button langBtn;

    ImageView callWaiterBtn;

    public static Order order;
    public static EditText tableNum;
    public static EditText coverNum;
    int table;
    int covers;

    private OrderApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_sections);

        Dialog d = createDialog();
        d.show();

        callWaiterBtn = (ImageView)findViewById(R.id.callWaiter);
        callWaiterBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "A Waiter is on their way!",
                            Toast.LENGTH_SHORT).show();
                }
        });
    }

    public Dialog createDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(SectionsActivity.this);

        View v = getLayoutInflater().inflate(R.layout.order_dialog, null);
        builder.setView(v);

        tableNum = (EditText)v.findViewById(R.id.numberEntry);
        coverNum = (EditText)v.findViewById(R.id.coversEntry);

        builder.setMessage("Order Information");
        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // Create order object in here
                    table = Integer.parseInt(tableNum.getText().toString());
                    covers = Integer.parseInt(coverNum.getText().toString());
                    Order.getInstance().setTableNumber(table);
                    Order.getInstance().setCoverNumber(covers);
                    Toast.makeText(SectionsActivity.this, "Order Created",
                            Toast.LENGTH_SHORT).show();
                }
        });

        return builder.create();
    }
}

这是Order类:

public class Order {

    int tableNumber;
    int coverNumber;
    double amount;
    List<Dish> orderItems;

    private static Order instance;

    // Constructors hidden because it is singleton...
    private Order(int tableNumber, int coverNumber, double amount,
            List<Dish> orderItems) {
        super();
        this.tableNumber = tableNumber;
        this.coverNumber = coverNumber;
        this.amount = amount;
        this.orderItems = orderItems;
    }

    private Order() {
        super();
        orderItems = new ArrayList<Dish>();
    }

    // SET THE INSTANCE
    public static void initInstance() {
        if (instance == null) {
            instance = new Order();
        }
    }

    // RETURN INSTANCE
    public static Order getInstance(){
        return instance;
    }

    // Getters and Setters
    public int getTableNumber() {
        return tableNumber;
    }

    public void setTableNumber(int tableNumber) {
        this.tableNumber = tableNumber;
    }

    public int getCoverNumber() {
        return coverNumber;
    }

    public void setCoverNumber(int coverNumber) {
        this.coverNumber = coverNumber;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public List<Dish> getOrderItems() {
        return orderItems;
    }

    // Add to order
    public void addToOrder(Dish d){
        this.orderItems.add(d);
    }

    public void removeFromOrder(int position){
        this.orderItems.remove(position);
    }

    public void setOrderItems(List<Dish> orderItems) {
        this.orderItems = orderItems;
    }
}

这是OrderApplication类:

public class OrderApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        initSingletons();
    }

    protected void initSingletons(){
        Order.initInstance();
    }
}

谁能看到我哪里出错了?任何建议都非常感谢!

STACK TRACE:

02-21 04:06:14.559: E/AndroidRuntime(29562): FATAL EXCEPTION: main
02-21 04:06:14.559: E/AndroidRuntime(29562): java.lang.NullPointerException
02-21 04:06:14.559: E/AndroidRuntime(29562):    at com.example.waitronproto9.SectionsActivity$2.onClick(SectionsActivity.java:80)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at android.os.Looper.loop(Looper.java:137)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at android.app.ActivityThread.main(ActivityThread.java:5041)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at java.lang.reflect.Method.invokeNative(Native Method)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at java.lang.reflect.Method.invoke(Method.java:511)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 04:06:14.559: E/AndroidRuntime(29562):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您尚未实例化Order。在尝试访问其方法之前,您需要这样做。

Order课程中尝试这样的事情: -

//RETURN INSTANCE
public static Order getInstance(){
    initInstance(); // Instantiate and then return.
    return instance;
}

<强>建议: -

SectionsActivity中,由于您已声明public static Order order;,为什么不这样使用它: -

order = Order.getInstance();
order.setTableNumber(table);

答案 1 :(得分:0)

在getInsance()中调用initInstance()。

虽然,您可能想重新考虑使用单身人士。关于这是否是好的做法存在争议,如果没有看到整个申请,我就不会发表评论。

答案 2 :(得分:0)

R.Js建议确实消除了NPE,但是你的代码也应该工作,因为OrderApplication在创建时会调用Order.initInstance(),因此一旦SectionsActivity启动就应该实例化单例。 除非你当然没有在mainfest.xml中声明OrderApplication,这可能是NPE的根本原因:

<application
    android:name="OrderApplication"