6 Replies - 944 Views - Last Post: 23 March 2015 - 07:19 PM Rate Topic: -----

#1 rocketmanhigh   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 21-August 14

NullPointer exception. Client/Server Program

Posted 23 March 2015 - 04:05 PM

Ian trying to run a Server program who accepts data from a client program after firing and actiion event listener on jButton. Yet i keep getting a nullpointer exception when trying to submit data to the server/ I have made sure to use global variables to avoid this problem but im still having issues below is the server program that calculates loan payments along with the client program and the exception errors that i get...... thank you so much in advance.



SERVER program that calculates loan amounts
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;

public class LoanServer extends JFrame{

JTextArea jta = new JTextArea();

public static void main(String []args){
new LoanServer();

}


public LoanServer(){

setLayout(new BorderLayout());
add(new JScrollPane(jta));

setTitle(  "LoanServer");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

try {
   
   ServerSocket serverSocket = new ServerSocket(8000);
   jta.append("LoanServer started at " + new Date () + "\n");
   System.out.print("Server Started");
   
   int clientNo= 1;
    while(true){
    
    Socket socket=serverSocket.accept();
    
   

    jta.append("Starting thread for client number "+ clientNo + " at " 
    + new Date() + "\n");
    
    InetAddress address = socket.getInetAddress();
    
    jta.append ("Client " + clientNo + " 's host name is " + address.getHostName()+ "\n");
    jta.append ("Client" + clientNo + "'s IP Address is " + address.getHostAddress() + "\n");
    
    
    HandleAClientTask task = new HandleAClientTask(socket);
    
    new Thread(task).start();
    
    clientNo++;
    
    }
    }
    catch(IOException ex){
    System.err.println(ex);
    
    }
    }
    
    
    class HandleAClientTask implements Runnable {
    
    private Socket socket;
    
    public HandleAClientTask(Socket socket){
    
    this.socket=socket;
    }
    
    @Override 
    
    public void run(){
  
      try{
      
      DataInputStream inputFromClient  = new DataInputStream(socket.getInputStream());
      
      DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
      
     
      while(true){
      
      double InterestRate = inputFromClient.readDouble();
      
      double numberOfYears=inputFromClient.readDouble();
      
      double loanAmount=inputFromClient.readDouble();
      
      
      
      double MonthlyInterestRate = InterestRate/1200;
      
      double MonthlyPayment =loanAmount * MonthlyInterestRate / (1 -
      (1 / Math.pow(1 + MonthlyInterestRate, numberOfYears * 12)));
      
      double totalPayment = MonthlyPayment * numberOfYears * 12;
      
      jta.append(" Interest Rate recieved from client: " + InterestRate + "\n");
      jta.append(" Number of Years recieved from client: " + numberOfYears + "\n");
     jta.append(" Loan Amount  recieved from client: " + loanAmount + "\n");
     
   jta.append(" Monthly Payment " + MonthlyPayment + "\n");
   
   jta.append (" Total Payment " + totalPayment  + "\n");
   
   outputToClient.writeDouble(MonthlyPayment);
   outputToClient.writeDouble(totalPayment);

   
   
   
  }
  
  }
  catch(IOException e ){
  System.err.println(e);
  }
  
  }
  }
  }
  




CLIENT program that passes loan information to server
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class LoanClient extends JFrame{

JTextArea jta= new JTextArea();
JTextField jtf1= new JTextField();
JTextField jtf2= new JTextField();
JTextField jtf3= new JTextField();
JButton submit= new JButton("submit");
 DataOutputStream toServer;
 DataInputStream fromServer;
 

public static void main(String [] args){

new LoanClient();

}

public LoanClient(){

JPanel p = new JPanel(new GridLayout(3,2));

p.add(new JLabel("Annual Interest Rate"));
p.add(jtf1);

p.add(new JLabel("NUmber of Years"));
p.add(jtf2);

p.add(new JLabel("Loan Amount"));
p.add(jtf3);


JPanel submitPanel= new JPanel(new BorderLayout());
submitPanel.add(submit,BorderLayout.CENTER);

JPanel topPanel  = new JPanel(new FlowLayout());

topPanel.add(p);
topPanel.add(submitPanel);

setLayout(new BorderLayout());

add(topPanel,BorderLayout.NORTH);
add(new JScrollPane(jta),BorderLayout.CENTER);

submit.addActionListener(new Listener());

    setTitle("Client");
    setSize(500, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    
    
    try{
    
    Socket socket= new Socket("local host",8000);
    
    fromServer= new DataInputStream(socket.getInputStream());
    
    
    toServer = new DataOutputStream(socket.getOutputStream());
    }
    catch(IOException ex){
    jta.append(ex.toString() + "\n");
    }
 }


private class Listener implements ActionListener{

@Override 

public void actionPerformed(ActionEvent e){

try{
 double AnnualInterestRate = Double.parseDouble(jtf1.getText().trim());

 double numberofyears= Double.parseDouble(jtf2.getText().trim());

 double loanAmount = Double.parseDouble(jtf3.getText().trim());

toServer.writeDouble(AnnualInterestRate);
toServer.flush();
toServer.writeDouble(numberofyears);
toServer.flush();

toServer.writeDouble(loanAmount);
toServer.flush();



double MonthlyPayment= fromServer.readDouble();
double totalPayment=fromServer.readDouble();

jta.append(" Annual Interest Rate " + AnnualInterestRate +"\n");
jta.append(" Number of years " + numberofyears +"\n");
jta.append(" loanAmount " + loanAmount  +"\n");

jta.append(" Monthly Payment " + MonthlyPayment +"\n");
jta.append(" Total Payment  " + totalPayment  +"\n");

}
catch(IOException ex){
System.out.println(ex.toString());
}

}
}
}



EXCEPTIONS

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at LoanClient$Listener.actionPerformed(LoanClient.java:89)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6525)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6290)
	at java.awt.Container.processEvent(Container.java:2234)
	at java.awt.Component.dispatchEventImpl(Component.java:4881)
	at java.awt.Container.dispatchEventImpl(Container.java:2292)
	at java.awt.Component.dispatchEvent(Component.java:4703)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
	at java.awt.Container.dispatchEventImpl(Container.java:2278)
	at java.awt.window.dispatchEventImpl(window.java:2739)
	at java.awt.Component.dispatchEvent(Component.java:4703)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
	at java.awt.EventQueue.access$400(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:697)
	at java.awt.EventQueue$3.run(EventQueue.java:691)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:719)
	at java.awt.EventQueue$4.run(EventQueue.java:717)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


Is This A Good Question/Topic? 0
  • +

Replies To: NullPointer exception. Client/Server Program

#2 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: NullPointer exception. Client/Server Program

Posted 23 March 2015 - 05:11 PM

Can you confirm that after
067	    toServer = new DataOutputStream(socket.getOutputStream());

toServer is not null?
System.out.println(toServer == null);

Was This Post Helpful? 1
  • +
  • -

#3 rocketmanhigh   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 21-August 14

Re: NullPointer exception. Client/Server Program

Posted 23 March 2015 - 05:18 PM

Im sorry im not understanding what you mean.
I added the line
[code] System.out.println(toServer == null);
[\code]

I didn't get anything on the console
Was This Post Helpful? 0
  • +
  • -

#4 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: NullPointer exception. Client/Server Program

Posted 23 March 2015 - 05:25 PM

The null pointer exception suggests that something is null, and it looks like it is the toServer reference. The suggestion was to place that print statement directly after the code I mentioned, to see if it is null. If you are getting nothing on the console, it might be getting that far in the try block.

in the catch block, if you do ex.printStackTrace() what do you get?
Was This Post Helpful? 1
  • +
  • -

#5 rocketmanhigh   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 21-August 14

Re: NullPointer exception. Client/Server Program

Posted 23 March 2015 - 05:28 PM

oh ok after inserting ex.printStackTrace()


i get
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at LoanClient$Listener.actionPerformed(LoanClient.java:91)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6525)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6290)
	at java.awt.Container.processEvent(Container.java:2234)
	at java.awt.Component.dispatchEventImpl(Component.java:4881)
	at java.awt.Container.dispatchEventImpl(Container.java:2292)
	at java.awt.Component.dispatchEvent(Component.java:4703)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
	at java.awt.Container.dispatchEventImpl(Container.java:2278)
	at java.awt.window.dispatchEventImpl(window.java:2739)
	at java.awt.Component.dispatchEvent(Component.java:4703)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
	at java.awt.EventQueue.access$400(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:697)
	at java.awt.EventQueue$3.run(EventQueue.java:691)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:719)
	at java.awt.EventQueue$4.run(EventQueue.java:717)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)



Was This Post Helpful? 0
  • +
  • -

#6 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: NullPointer exception. Client/Server Program

Posted 23 March 2015 - 05:46 PM

I was looking at this code here, when referring to adding the stacktrace:
060	    try{
061	     
062	    Socket socket= new Socket("local host",8000);
063	     
064	    fromServer= new DataInputStream(socket.getInputStream());
065	     
066	     
067	    toServer = new DataOutputStream(socket.getOutputStream());
068	    }
069	    catch(IOException ex){
070	       jta.append(ex.toString() + "\n");
               // ex.printStackTrace();
071	    }


Anyway, it should be localhost, not local host. Whether that will fix everything I'm not sure, but it's a starting point.

Whenever you catch an exception, always print the stack trace, it tells you exactly what went wrong. There is a tutorial on this site over here which might be of some interest to you.
Was This Post Helpful? 1
  • +
  • -

#7 rocketmanhigh   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 21-August 14

Re: NullPointer exception. Client/Server Program

Posted 23 March 2015 - 07:19 PM

Ok thanks for the advice i will do that form now on. As for this program it is still not working after i fixed "localhost"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1