-1

我得到一个NullPointerException如下:

java.lang.NullPointerException  
    file:/K:/Learner/JavaFx2/ProductApplication/dist/run166129449/ProductApplication.jar!/com/product/app/view/viewsingle.fxml  
      at com.product.app.controller.ViewSingleController.initialize(ViewSingleController.java:70)  
      at javafx.fxml.FXMLLoader.load(Unknown Source)
      at javafx.fxml.FXMLLoader.load(Unknown Source)

ViewSingleController的如下:

package com.product.app.controller;  
import com.product.app.model.Product;  
import com.product.app.service.ViewProductsService;  
import com.product.app.util.JSONParser;  
import com.product.app.util.TagConstants;  
import java.net.URL;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.ResourceBundle;  
import javafx.collections.ObservableList;  
import javafx.concurrent.Service;  
import javafx.concurrent.Task;  
import javafx.event.ActionEvent;  
import javafx.fxml.FXML;  
import javafx.fxml.Initializable;  
import javafx.scene.control.ProgressIndicator;  
import javafx.scene.control.TextArea;  
import javafx.scene.control.TextField;  
import javafx.scene.layout.Region;  
import javafx.stage.Stage;  
import javax.swing.JOptionPane;  
import org.apache.http.NameValuePair;  
import org.apache.http.message.BasicNameValuePair;  
import org.json.JSONArray;  
import org.json.JSONObject;  
/** 
 * FXML Controller class 
 * 
 * @author Arun Joseph 
 */  
public class ViewSingleController implements Initializable {  
    private static String action = "";  
    @FXML  
    private TextField txtID;  
    @FXML  
    private TextField txtName;  
    @FXML  
    private TextField txtPrice;  
    @FXML  
    private TextArea txtDesc;  
    @FXML  
    private Region veil;  
    @FXML  
    private ProgressIndicator p;  
    private ViewProductsService service = new ViewProductsService();  
    private JSONObject product = null;  
    private JSONParser parser = new JSONParser();  
    private int pid = 1;  
    public void setPid(int pid) {  
        this.pid = pid;  
    }  
    /** 
     * Initializes the controller class. 
     */  
    @Override  
    public void initialize(URL url, ResourceBundle rb) {  
        veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)");  
        p.setMaxSize(150, 150);  
        p.progressProperty().bind(service.progressProperty());  
        veil.visibleProperty().bind(service.runningProperty());  
        p.visibleProperty().bind(service.runningProperty());  
        Product product = new Product();  
        service.start();  
        ObservableList<Product> products = service.valueProperty().get();  
        products.get(pid);  
        txtID.textProperty().set(String.valueOf(products.get(pid).getPid()));  
        //product = service.valueProperty().get().get(pid);  
        //txtID.setText(String.valueOf(product.getPid()));  
        txtName.textProperty().set(product.getName());  
        txtPrice.textProperty().set(String.valueOf(product.getPrize()));  
        txtDesc.textProperty().set(product.getDescription());  
    }  
    private SomeService someService = new SomeService();  
    @FXML  
    private void handleUpdateButtonClick(ActionEvent event) {  
        action = "update";  
        someService.start();  
        p.progressProperty().bind(service.progressProperty());  
        veil.visibleProperty().bind(service.runningProperty());  
        p.visibleProperty().bind(service.runningProperty());  
    }  
    @FXML  
    private void handleDeleteButtonClick(ActionEvent event) {  
        action = "delete";  
        someService.start();  
        p.progressProperty().bind(service.progressProperty());  
        veil.visibleProperty().bind(service.runningProperty());  
        p.visibleProperty().bind(service.runningProperty());  
    }  
    @FXML  
    private void handleCancelButtonClick(ActionEvent event) {  
        closeStage();  
    }  
    private void closeStage() {  
        ViewSingleController.stage.close();  
    }  
    private static Stage stage = null;  
    public static void setStage(Stage stage) {  
        ViewSingleController.stage = stage;  
    }  
    private class SomeService extends Service<String> {  
        @Override  
        protected Task<String> createTask() {  
            return new SomeTask();  
        }  
        private class SomeTask extends Task<String> {  
            @Override  
            protected String call() throws Exception {  
                String result = "";  
                int success = 0;  
                List<NameValuePair> params = new ArrayList<NameValuePair>();  
                switch (action) {  
                    case "update":  
                        params.add(new BasicNameValuePair("pid", txtID.getText()));  
                        params.add(new BasicNameValuePair("name", txtName.getText()));  
                        params.add(new BasicNameValuePair("price", txtPrice.getText()));  
                        params.add(new BasicNameValuePair("description", txtDesc.getText()));  
                        product = parser.makeHttpRequest(TagConstants.url_update_product_with_id, "POST", params);  
                        success = product.getInt(TagConstants.TAG_SUCCESS);  
                        if (success == 1) {  
                            result = "Successfully Updated the product";  
                            JOptionPane.showMessageDialog(null, result);  
                            closeStage();  
                        }  
                        break;  
                    case "delete":  
                        params.add(new BasicNameValuePair("pid", txtID.getText()));  
                        product = parser.makeHttpRequest(TagConstants.url_delete_product_with_id, "POST", params);  
                        success = product.getInt(TagConstants.TAG_SUCCESS);  
                        if (success == 1) {  
                            result = "Successfully Deleted the product";  
                            JOptionPane.showMessageDialog(null, result);  
                            closeStage();  
                        }  
                        break;  
                }  
                return result;  
            }  
        }  
    }  
}  

请帮助我解决这个空指针问题真的需要帮助。先感谢您

4

2 回答 2

0

检查线路ViewSingleController.java:70。在那里放置一个断点。在调试器中运行程序并查看哪些变量/字段为空。

于 2013-11-01T13:08:30.710 回答
0

异常发生在第 70 行,正如您从 StackTrace 中看到的那样。在第 70 行,您调用:

txtPrice.textProperty().set(String.valueOf(product.getPrize()));

NullPointerException 意味着您正在尝试访问不存在的对象的方法。在这里,这可能是 txtPrice、textProperty、product 或 getPrize。浏览您的代码,我它可能是 txtPrice,因为您仅通过将其设置为成员变量

private TextField txtPrice; 

但你永远不会初始化它。因此,txtPrice 为 null 并且 txtPrice.textProperty().set 可能会抛出 NullPointer。

于 2013-11-01T13:21:14.450 回答