Bookmark Topic Watch Topic
  • New Topic

Urgent!!!A NullPointerException !!! I can't solve it.  RSS feed

 
Chrono Wren
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have complied 3 files (SerialBean.java;SerialBuffer.java;ReaderSerial.java) as my package successfully, it is named serial. The package extends javax.comm.*; I wanna do some serial work.
Then I maked an application :SerialExample1
--------------------------------------code----------------------------------
----------------------------SerialExmaple1.java-----------------------------
import serial.*;
import java.io.*;
import javax.comm.*;
public class SerialExample1
{
public static void main(String args[])
{
SerialBean sB=new SerialBean(1);
sB.Initialize();
sB.WritePort("Hi,Chrono");
sB.ClosePort();
}
}
-----------------------------------------------------------------------------
It is good enough to deliver the String "Hi,Chrono".
Then I maked another windows application :SerialExample2
------------------------------------code-------------------------------------
-----------------------------Frame1.java-------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import serial.*;
import java.io.*;
import javax.comm.*;
public class Frame1 extends JFrame
{
private JPanel contentPane;
private XYLayout xYLayout1 = new XYLayout();
private Button button1 = new Button();
private TextField textField1 = new TextField();
private SerialBean sB=new SerialBean(1);
//Construct the frame
public Frame1()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
button1.setLabel("button1");
button1.addActionListener(new Frame1_button1_actionAdapter(this));
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
textField1.setText("textField1");
contentPane.add(button1, new XYConstraints(54, 52, -1, -1));
contentPane.add(textField1, new XYConstraints(195, 57, -1, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
void button1_actionPerformed(ActionEvent e)
{
sB.Initialize();
sB.WritePort("Hi Chrono");
sB.ClosePort();
}
}
class Frame1_button1_actionAdapter implements java.awt.event.ActionListener
{
private Frame1 adaptee;
Frame1_button1_actionAdapter(Frame1 adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.button1_actionPerformed(e);
}
}
------------------------------------------------------------------------------
---------------------------SerialExample2.java--------------------------------
import javax.swing.UIManager;
import java.awt.*;
public class SerialExample2
{
private boolean packFrame = false;
//Construct the application
public SerialExample2()
{
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
{
frame.pack();
}
else
{
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
new SerialExample2();
}
}
-----------------------------------------------------------------------------------

It is a bad code!!! I compiled them seccessfully but It can't work .The java system gave
some wrong messages
-----------------------------------------------------------------------------------
Exception occurred during event dispatching:java.lang.NullPointerExceptionat serial.SerialBean.WritePort(SerialBean.java:115)at Frame1.button1_actionPerformed(Frame1.java:66)at Frame1_button1_actionAdapter.actionPerformed(Frame1.java:82)at java.awt.Button.processActionEvent(Button.java:329)at java.awt.Button.processEvent(Button.java:302)at java.awt.Component.dispatchEventImpl(Component.java:2593)at java.awt.Component.dispatchEvent(Component.java:2497)at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)-----------------------------------------------------------------------------------
I tried to debug it in Frame1.java:66 and SerialBean.java:115 but I can not find any error.
How to deal with it?
Here are the code about package serial
-----------------------------------code--------------------------------------------
----------------------------SerialBean.java----------------------------------------
package serial;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* This bean provides some basic functions to implement full dulplex
* information exchange through the srial port.
*
*/
public class SerialBean
{
static String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
static OutputStream out;
static InputStream in;
SerialBuffer SB;
ReadSerial RT;
/**
*
* Constructor
*
* @param PortID the ID of the serial to be used. 1 for COM1,
* 2 for COM2, etc.
*
*/
public SerialBean(int PortID)
{
PortName = "COM" + PortID;
}
/**
*
* This function initialize the serial port for communication. It startss a
* thread which consistently monitors the serial port. Any signal capturred
* from the serial port is stored into a buffer area.
*
*/
public int Initialize()
{
int InitSuccess = 1;
int InitFail = -1;
try
{
portId = CommPortIdentifier.getPortIdentifier(PortName);
try
{
serialPort = (SerialPort)
portId.open("Serial_Communication", 2000);
} catch (PortInUseException e)
{
return InitFail;
}
//Use InputStream in to read from the serial port, and OutputStream
//out to write to the serial port.
try
{
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e)
{
return InitFail;
}
//Initialize the communication parameters to 9600, 8, 1, none.
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e)
{
return InitFail;
}
} catch (NoSuchPortException e)
{
return InitFail;
}
// when successfully open the serial port, create a new serial buffer,
// then create a thread that consistently accepts incoming signals from
// the serial port. Incoming signals are stored in the serial buffer.
SB = new SerialBuffer();
RT = new ReadSerial(SB, in);
RT.start();
// return success information
return InitSuccess;
}
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public String ReadPort(int Length)
{
String Msg;
Msg = SB.GetMsg(Length);
return Msg;
}
/**
*
* This function sends a message through the serial port.
*
* @param Msg The string to be sent.
*
*/
public void WritePort(String Msg)
{
int c;
try
{
for (int i = 0; i < Msg.length(); i++)
out.write(Msg.charAt(i));
} catch (IOException e) {}
}
/**
*
* This function closes the serial port in use.
*
*/
public void ClosePort()
{
RT.stop();
serialPort.close();
}
}
---------------------------------------------------------------------------------
----------------------------ReadSerial.java--------------------------------------
package serial;
import java.io.*;
/**
*
* This class reads message from the specific serial port and save
* the message to the serial buffer.
*
*/
public class ReadSerial extends Thread
{
private SerialBuffer ComBuffer;
private InputStream ComPort;
/**
*
* Constructor
*
* @param SB The buffer to save the incoming messages.
* @param Port The InputStream from the specific serial port.
*
*/
public ReadSerial(SerialBuffer SB, InputStream Port)
{
ComBuffer = SB;
ComPort = Port;
}
public void run()
{
int c;
try
{
while (true)
{
c = ComPort.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {}
}
}
------------------------------------------------------------------------------------
------------------------------SerialBuffer.java-------------------------------------
package serial;
public class SerialBuffer
{
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
private int LengthNeeded = 1;
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public synchronized String GetMsg(int Length)
{
LengthNeeded = Length;
notifyAll();
if (LengthNeeded > Content.length())
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
}
CurrentMsg = Content.substring(0, LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();
return CurrentMsg;
}
/**
*
* This function stores a character captured from the serial port to the
* buffer area.
*
* @param t The char value of the character to be stored.
*
*/
public synchronized void PutChar(int c)
{
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (LengthNeeded < Content.length())
{
available = true;
}
notifyAll();
}
}
-----------------------------------------------------------------------------------
Help me !!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't post the same question in multiple forums.
This has already been addressed here in the JiG (advanced) forum, so I'm going to close this thread and direct people there if they have a view.
Dave
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!