0

I keep getting this error message, and I've tried many different things to fix it. Can someone tell me what I have wrong? I had "null" after every private variable or method declaration I have on the code, but I even took that off thinking it was the issue but it was not. I have searched for the reason why this might but happening to my code but no luck.

This is a client/server interaction. And the other parts such as Server and BankAccount have no issues.

Exception in thread "main" java.lang.NullPointerException

at Client.(Client.java:64)

at Client.main(Client.java:105)

import java.net.*; 
import java.io.*; 
import java.io.InputStreamReader;


public class Client 
{ 
// initialize socket and variables 
private Socket socket;
private DataInputStream input;
private DataOutputStream output;

BufferedReader br;  
BankAccount testAccount = new BankAccount(1000.00);
double wAmount = 150.00;
double dAmount = 200.00;

String num = "0";

// constructor to put IP address and port 
public Client(String address, int port) 
{ 
    // establish a connection 
    try
    { 
        socket = new Socket(address, port);
        System.out.println("");
        System.out.println("---------------------------------------------------------------------");
        System.out.println("Connection Sucessful");
        System.out.println("---------------------------------------------------------------------");
        
        //once connection is established, bring up banking options
        System.out.println("Bank Mode Activated");
        System.out.println("Enter the number choice of what you'd like to do:");
        System.out.println("1. Withdraw");
        System.out.println("2. Deposit");
        System.out.println("3. Check Balance");
        System.out.println("4. To end the program");
        System.out.println("--------------------------------------------------------------------");

        // initializes input stream and takes input from terminal
        input = new DataInputStream(System.in);
        // sends output to the socket
        output = new DataOutputStream(socket.getOutputStream());
        
    }
    catch(UnknownHostException unKnown) 
    { 
        System.out.println(unKnown); 
    } 
    catch(IOException except) 
    { 
        System.out.println(except); 
    }
    
    
    while(!num.equals("4")){
        try{
            num = br.readLine();  //LINE 64
            output.writeUTF(num);
            
        if(num.equals("1")){
            testAccount.withdraw(wAmount);
            System.out.println("This is the balance after this transaction: $" + testAccount.getBalance());
        }
        if(num.equals("2")){
            testAccount.deposit(dAmount);
            System.out.println("This is the balance after this transaction: $" + testAccount.getBalance());
        }
        if(num.equals("3")){
            System.out.println("This is your account balance: $" + testAccount.getBalance());
        }
        if(num.equals("4")){
            System.out.println("Thank you!");
            System.out.println("User is finished.");
        }
        else{
            System.out.println("Invalid. Please try again.");
        }
        } //end of try
        catch(IOException i){
            System.out.println(i); 
        }           
    }
    // close the connection 
    try
    { 
        input.close(); 
        output.close(); 
        socket.close(); 
    } 
    catch(IOException i) 
    { 
       System.out.println(i); 
    }
}

public static void main(String args[]) 
{ 
    Client client = new Client("127.0.0.1", 5000); //LINE 105
} 

}

  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Dren Feb 24 '21 at 21:56
  • Please clarify, maybe with some comment in your code, which is the line actual line you are seeing in the stacktrace, just by the line number it's hard to see. – tmilar Feb 24 '21 at 21:59
  • `br` is never initialized. Also, don't catch exceptions and continue on as if nothing happened. – Mark Rotteveel Feb 25 '21 at 19:47

1 Answers1

2

Looks like your br class member isn't initialized, and it's always null.
Consider

BufferedReader br = new BufferedReader(...);

See Java docs for details

peter
  • 583
  • 2
  • 11