Zamieszczony kod ma mnóstwo błędów, między innymi instrukcje poza metodami. Trochę go wygładziłem i działa.
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Klient extends JFrame
{
JButton magazyn;
BufferedReader klawiatura;
PrintWriter out;
private Socket socketKlienta;
private BufferedReader in;
ObjectOutputStream OOS=null;
ObjectInputStream OIS=null;
Magazyn m3 = new Magazyn();
String nazwaPliku="E:\\Pliczek.txt";
Klient()
{
try
{
socketKlienta = new Socket("localhost", 2020);
}
catch (IOException e)
{
System.out.println("Uruchom serwer: " + e);
System.exit(1);
}
magazyn = new JButton("Magazyn");
magazyn.addActionListener(e -> send());
add(magazyn);
}
public static void main(String[] args) throws Exception
{
Klient okno = new Klient();
//okno.init();
okno.pack();
okno.setLocationRelativeTo(null);
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setVisible(true);
//Klient client = new Klient();
}
void utworzStrumienie()
{
String messageFromServer = null;
try
{
OIS = new ObjectInputStream(socketKlienta.getInputStream());
OOS = new ObjectOutputStream(socketKlienta.getOutputStream());
System.out.println("Strumienie utworzone");
}
catch(Exception e)
{
System.out.println(e);
}
}
void wyslij()
{
String messageFromServer = null;
try
{
OOS.writeObject("123");
}
catch(Exception e)
{
System.out.println(e);;
}
System.out.println("wyslalem ");
}
void send()
{
try
{
String messageFromServer = null;
utworzStrumienie();
wyslij();
/////////////////////tutaj wystepuje bląd NullPointerException
System.out.println("wyslalem ");
}
catch (Exception ex)
{
System.out.println(ex);;
}
}
}