Я разработал простое клиентское приложение сокет-сервер. Только когда я запускаю проект, он работает, только если код находится в методе run, а IP-адрес уже определен в коде. Я нашел исправление, которое позволит мне самому ввести IP-адрес, однако он очень темпераментный и часто терпит неудачу, за исключением ниже.
package nxs;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import javax.swing.JOptionPane;
import static nxs.nx.s;
/**
*
* @author kyleg
*/
public class control extends javax.swing.JFrame {
static Socket s;
static DataOutputStream dout;
static DataInputStream din;
/**
* Creates new form contol
*/
public control() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(control.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(control.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(control.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(control.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new control().setVisible(true);
}
}); String msgin = "";
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 1201:");
if(!serverAddress.equals("")){try{
s = new Socket(serverAddress,1201);
din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
while(!msgin.equals("exit")){
msgin = din.readUTF();
}
}
catch(Exception e){}}
}
// Variables declaration - do not modify
// End of variables declaration
}
Исключение:
Исключение в потоке "AWT-EventQueue-0" java.lang.NullPointerException
в javax.swing.plaf.nimbus.NimbusStyle.validate (NimbusStyle.java:298)
в
javax.swing.plaf.nimbus.NimbusStyle.getValues (NimbusStyle.java:806)
в
javax.swing.plaf.nimbus.NimbusStyle.getInsets (NimbusStyle.java:485)
в
javax.swing.plaf.synth.SynthStyle.installDefaults (SynthStyle.java:913)
в
javax.swing.plaf.synth.SynthLookAndFeel.updateStyle (SynthLookAndFeel.java:265)
в
javax.swing.plaf.synth.SynthPanelUI.updateStyle (SynthPanelUI.java:117)
в
javax.swing.plaf.synth.SynthPanelUI.installDefaults (SynthPanelUI.java:100)
в
javax.swing.plaf.basic.BasicPanelUI.installUI (BasicPanelUI.java:56)
в
javax.swing.plaf.synth.SynthPanelUI.installUI (SynthPanelUI.java:62)
в javax.swing.JComponent.setUI (JComponent.java:666) в
javax.swing.JPanel.setUI (JPanel.java:153) в
javax.swing.JPanel.updateUI (JPanel.java:126) в
javax.swing.JPanel. (JPanel.java:86) в
javax.swing.JPanel. (JPanel.java:109) в
javax.swing.JPanel. (JPanel.java:117) в
javax.swing.JRootPane.createGlassPane (JRootPane.java:546) в
javax.swing.JRootPane. (JRootPane.java:366) в
javax.swing.JFrame.createRootPane (JFrame.java:286) в
javax.swing.JFrame.frameInit (JFrame.java:267) в
javax.swing.JFrame. (JFrame.java:190) в
nxs.control. (control.java:27) в
nxs.control $ 1.run (control.java:87) в
java.awt.event.InvocationEvent.dispatch (InvocationEvent.java:311) в
java.awt.EventQueue.dispatchEventImpl (EventQueue.java:756) в
java.awt.EventQueue.access $ 500 (EventQueue.java:97) в
java.awt.EventQueue $ 3.run (EventQueue.java:709) в
java.awt.EventQueue $ 3.run (EventQueue.java:703) в
java.security.AccessController.doPrivileged (собственный метод) в
java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:80)
в java.awt.EventQueue.dispatchEvent (EventQueue.java:726) в
java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java:201)
в
java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java:116)
в
java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:105)
в
java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:101)
в
java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:93)
в java.awt.EventDispatchThread.run (EventDispatchThread.java:82)
Исключение в потоке "main" java.lang.NullPointerException в nxs.control.main (control.java:95)
C: \ Users \ kyleg \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ исполнитель-сниппеты \ run.xml: 53:
Ява вернулась: 1
СБОЙ СТРОИТ (общее время: 4 секунды)