0

I am getting null pointer exception in my program, I don't know why. I have a JavaFX UI, which is giving me a few constraint information for clients to upload file, and I am getting them as an instance of a Constraints class in the thread class. When I pick the instance and call methods to see values, I can see them, but inside run method, when I try to send them over socket, I print them and see nothing. Here is my code for UI:

package Server;

/**
* Created by Ahmed on 15/03/2017 at 10:26 PM.
*/
/**
* Sample Skeleton for 'serverHome.fxml' Controller Class
*/

import constraintPackage.Constraints;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;

public class serverHomeUI{
private WorkerThread workerThread = new WorkerThread();
private serverHome serverHome; //this is insignificant here
private boolean fileTypeButtonSelection = false;
private boolean numFileButtonSelection = false;
private boolean fileSizeButtonSelection = false;
private boolean delayButtonSelection = false;
private Constraints constraints = new Constraints();
void setServerHome(serverHome serverHome, String path){
    this.serverHome = serverHome;
    rootLocation.setText(path);
}

/*void setWorkerThread(WorkerThread workerThread){
    this.workerThread = workerThread;
}*/

@FXML // fx:id="rootLocation"
private TextField rootLocation; // Value injected by FXMLLoader

@FXML // fx:id="browseButton"
private Button browseButton; // Value injected by FXMLLoader

@FXML // fx:id="cFile"
private CheckBox cFile; // Value injected by FXMLLoader

@FXML // fx:id="cppFile"
private CheckBox cppFile; // Value injected by FXMLLoader

@FXML // fx:id="javaFile"
private CheckBox javaFile; // Value injected by FXMLLoader

@FXML // fx:id="pyFile"
private CheckBox pyFile; // Value injected by FXMLLoader

@FXML // fx:id="phpFile"
private CheckBox phpFile; // Value injected by FXMLLoader

@FXML // fx:id="fileTypeButton"
private Button fileTypeButton; // Value injected by FXMLLoader

@FXML // fx:id="numFile"
private TextField numFile; // Value injected by FXMLLoader

@FXML // fx:id="numFileButton"
private Button numFileButton; // Value injected by FXMLLoader

@FXML // fx:id="maxFileSize"
private TextField maxFileSize; // Value injected by FXMLLoader

@FXML // fx:id="maxFileSizeButton"
private Button maxFileSizeButton; // Value injected by FXMLLoader

@FXML // fx:id="stdID"
private TextField stdID; // Value injected by FXMLLoader

@FXML // fx:id="stdIDButton"
private Button stdIDButton; // Value injected by FXMLLoader

@FXML // fx:id="delay"
private TextField delay; // Value injected by FXMLLoader

@FXML // fx:id="delayButton"
private Button delayButton; // Value injected by FXMLLoader

@FXML
void browseButtonFired(ActionEvent event) {
    DirectoryChooser directoryChooser = new DirectoryChooser();
    directoryChooser.setTitle("Choose Root Directory");
    Stage stage = serverHome.stage;
    File selectedDirectory = directoryChooser.showDialog(stage);
    if(selectedDirectory != null){
        rootLocation.setText(selectedDirectory.getAbsolutePath());
        serverHome.setRoot(rootLocation.getText());
    }
}

@FXML
void delayButtonFired(ActionEvent event) {
    if(delay.getText() != null){
        delayButtonSelection = true;
        constraints.setDelayTime(Integer.parseInt(delay.getText()));
        if(numFileButtonSelection && fileTypeButtonSelection && fileSizeButtonSelection && delayButtonSelection){
            workerThread.sendConstraints(constraints);
        }
    }
}

@FXML
void fileTypeButtonFired(ActionEvent event) {
    ArrayList<String> ticked = new ArrayList<>();
    int i = 0;
    if(cFile.isSelected() || cppFile.isSelected() || javaFile.isSelected() || pyFile.isSelected() || phpFile.isSelected()){
        if(cFile.isSelected()){
            ticked.add("c");
        }
        else if(!cFile.isSelected()){
            ticked.remove("c");
        }
        else if(cppFile.isSelected()){
            ticked.add("cpp");
        }

        else if(!cppFile.isSelected()){
            ticked.remove("cpp");
        }

        else if(javaFile.isSelected()){
            ticked.add("java");
        }

        else if(!javaFile.isSelected()){
            ticked.remove("java");
        }

        else if(pyFile.isSelected()){
            ticked.add("py");
        }

        else if(!pyFile.isSelected()){
            ticked.remove("py");
        }

        else if(phpFile.isSelected()){
            ticked.add("php");
        }

        else if(!phpFile.isSelected()){
            ticked.remove("php");
        }

        fileTypeButtonSelection = true;
        constraints.setFileTypes(ticked);
        if(numFileButtonSelection && fileTypeButtonSelection && fileSizeButtonSelection && delayButtonSelection){
            workerThread.sendConstraints(constraints);
        }
    }
}

@FXML
void maxFileSizeButtonFired(ActionEvent event) {
    if(maxFileSize.getText() != null){
        fileSizeButtonSelection = true;
        constraints.setMaxFileSize(Float.parseFloat(maxFileSize.getText()));
        if(numFileButtonSelection && fileTypeButtonSelection && fileSizeButtonSelection && delayButtonSelection){
            workerThread.sendConstraints(constraints);
        }
    }
}

@FXML
void numFileButtonFired(ActionEvent event) {
    if(numFile.getText() != null){
        numFileButtonSelection = true;
        constraints.setMaxNumFile(Integer.parseInt(numFile.getText()));
        if(numFileButtonSelection && fileTypeButtonSelection && fileSizeButtonSelection && delayButtonSelection){
            workerThread.sendConstraints(constraints);
        }
    }
}

@FXML
void stdIDButtonFired(ActionEvent event) {

}

}

And here is my code for the thread

class WorkerThread implements Runnable
{
private Hashtable<String, Integer> hashTable = new Hashtable<>();
private Socket socket;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private InputStreamReader inputStreamReader = null;
private BufferedReader bufferedReader = null;
private PrintWriter printWriter = null;
private ObjectOutputStream objectOutputStream = null;
private ObjectInputStream objectInputStream = null;
private constraintCreated = false;
private Constraints constraints1;
private int id = 0;

void sendConstraints(Constraints constraints){
    System.out.println("hello bai, constraint false");
    constraints1 = constraints;
    constraintCreated = true;
    System.out.println("constraint true");
    System.out.println(this.constraints1.getDelayTime());//these two lines give
    System.out.println(this.constraints1.getFileNum());//proper output
}

public WorkerThread(){

}
public WorkerThread(Socket s)
{
    this.socket = s;

    try {
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
        objectInputStream = new ObjectInputStream(inputStream);
        objectOutputStream = new ObjectOutputStream(outputStream);
        inputStreamReader= new InputStreamReader(inputStream);
        bufferedReader = new BufferedReader(inputStreamReader);
        printWriter = new PrintWriter(outputStream);

    } catch (IOException e) {
        e.printStackTrace();
    }

    String clientIP = s.getInetAddress().toString();

    String studentID = null;
    int stdID = 0;

    try {
        studentID = bufferedReader.readLine();
        stdID = Integer.parseInt(studentID);
    } catch (IOException e) {
        e.printStackTrace();
    }

    hashTable.put(clientIP, stdID);
    System.out.println(root1); //root1="G:\Akib Ahmed\Pdf\3-2\(CSE 321, 322) Computer Networks\Lab\Offline 1 (Code Repository)\src\Server\Root"
    File file = new File(root1);
    if(!file.exists()){
        boolean a = file.mkdir();
        file = new File(root1 + "\\"+stdID);
        if(!file.exists()) {
            boolean b = file.mkdir();
        }
    }

    this.id = id;
}

public void run() {
    Constraints constraints = new Constraints();

    if (printWriter != null) {
        printWriter.print("Your id is: " + this.id);
        printWriter.println();
        printWriter.flush();
    }

    String str = null;
    while (true) {
        try {
            System.out.println("inside while loops");
            str = bufferedReader.readLine();
            System.out.println(str);
            if (str != null) {
                if(str.equals("Constraints")){ //reading str by bufferedReader
                    System.out.println("going to send constraints");


                    //while(!constraintCreated) {if(constraints1 == null) System.out.println("something");} //this went on an inifinite loop
                    if(constraintCreated) System.out.println("really!"); //constraintCreated is false here, although I have assigned it true earlier
                    System.out.println(constraints1.getDelayTime()); //null pointer exception and compilation never goes further
                    ArrayList<String> filetyp = constraints1.getFileTypes();
                    objectOutputStream.writeObject(filetyp);
                    objectOutputStream.flush();
                    printWriter.write(String.valueOf(constraints1.getFileSize()));
                    printWriter.println();
                    printWriter.flush();
                    printWriter.write(String.valueOf(constraints1.getFileNum()));
                    printWriter.println();
                    printWriter.flush();
                    printWriter.write(String.valueOf(constraints1.getDelayTime()));
                    printWriter.println();
                    printWriter.flush();
                    System.out.println(constraints1.getDelayTime());
                    System.out.println("sent Constraints");
                }
        }
        else{
                System.out.println("here????");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Problem in communicating with the client [" + id + "]. Terminating worker thread.");
            break;
        }
    }

    ShowOff.workerThreadCount--;
    System.out.println("Client [" + id + "] is now terminating. No. of worker threads = "
            + ShowOff.workerThreadCount);
}
}
aahmed109
  • 161
  • 1
  • 1
  • 6
  • Apparently, even mentioning a null pointer exception gets your question closed as a duplicate. But, maybe trying making an MCVE. This is an awful lot of code, and much of it probably isn't relevant to the problem. – D M Mar 19 '17 at 21:29
  • ... and always post a stacktrace, something that is mentioned in most every question on NPE. – Hovercraft Full Of Eels Mar 19 '17 at 21:30
  • "Apparently, even mentioning a null pointer exception gets your question closed as a duplicate."-couldn't agree more, but this isn't a duplicate of a discussion type question, is it? – aahmed109 Mar 19 '17 at 21:34
  • If your question is "why does this method return `null`?" then you should ask the question again. (Realistically it's hard to get a question re-opened no matter how much editing you do.) `NullPointerException` is always the same: it's because you didn't check a return value and deferenced a null pointer. Period, the end. You should always check any return value that can be null. – markspace Mar 19 '17 at 22:09

0 Answers0