示例.java
class Sample
{
private BufferedOutputStream output;
public handleRequest(Socket socket)
{
this.output = new BufferedOutputStream(socket.getOutputStream(), BUFFSIZE);
sendString("sometext");//This is working fine
}
public void sendString(String str) throws IOException
{
ByteArrayOutputStream bytestream;
bytestream = new ByteArrayOutputStream(str.length());
DataOutputStream out;
out = new DataOutputStream(bytestream);
for (int i = 0; i < str.length(); i++)
out.write((byte) str.charAt(i));
output.write(bytestream.toByteArray(), 0, bytestream.size());
output.flush();
}
}
另一个类.java
class AnotherClass
{
public send()
{
Sample smp = new Sample();
smp.sendString("somestring");//This is not working
}
}
每当我在 AnotherClass.java 中调用 sendstring 方法时,它都会给我一个 NullPointerExcetpiton。我无法弄清楚。任何人都可以帮助我..提前谢谢....