我的问题是我有两个类,第一个是实现Runnable的实现类,第二个是扩展框架视图的interfaceView
但是当我尝试在实现类中创建interfaceView的对象时
例如
interfaceview check1 ;
check1.get_sms_validation();
它给了我
java.lang.IllegalArgumentException:空应用程序
例外
实现类的代码是
但是当我这样做
interfaceview check1 = new interfaceview () ;
check1.get_sms_validation();
它告诉我在interfaceview类中创建一个构造函数,当我这样做时,它给我一个错误
实际清单和正式清单的长度不同。
我的实现类的代码是
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package smsapp;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.smslib.GatewayException;
import org.smslib.TimeoutException;
import org.smslib.modem.*;
import org.smslib.InboundMessage;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Random;
import org.jdesktop.application.SingleFrameApplication;
/**
*
* @author ist
*/
public class Implementation implements Runnable {
private boolean login;
public static Service srv;
public static OutboundMessage outMsg;
public static String defaultPort="COM7";
public static ArrayList<OutboundMessage> queuedMessages = new ArrayList<OutboundMessage>();
static QueueSendingNotification queueSendingNotification = new QueueSendingNotification();
private boolean phoneConnected;
Thread t;
String msg ="1";
public String Data_For_File ;
public String messageFor_File = null;
public int check = 0;
Random generator;
public String message;
private SingleFrameApplication SingleFrameApplication;
// private String messageFor_File1;
public String messageFor_File1 = null;
// public InterfaceView = new InterfaceView ();
Implementation()
{
phoneConnected = false;
}
public boolean connectPhone(String comPort)
{
try
{
srv = new Service();
SerialModemGateway gateway = new SerialModemGateway("Modem 0", comPort, 9600, "", "");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
srv.addGateway(gateway);
}
catch(Exception e)
{
System.out.println("Could Not connect to the Modem. Please try again.\n");
}
this.phoneConnected = true;
return true;
}
public boolean startService()
{
try
{
if(phoneConnected)
{
srv.startService();
t = new Thread(this);
t.start();
generator = new Random( 19580427 );
}
else
{
return false;
}
}
catch(Exception e)
{
try
{
System.out.println("Service Start Error ");
e.printStackTrace();
srv.stopService();
return false;
}catch(Exception ee)
{
System.out.println("Service Stop Error");
e.printStackTrace();
return false;
}
}
srv.setOutboundNotification(queueSendingNotification);
// myTimer = new Timer (10000,runProcess);
// myTimer.setInitialDelay(0);
// myTimer.start();
return true;
}
public boolean stopService()
{
try
{
srv.stopService();
t.stop();
}catch(Exception e)
{
System.out.println("There was an error in disconnecting.");
e.printStackTrace();
return false;
}
return true;
}
public void run()
{
// processOutMessage("1");
// processOutMessage();
while (true)
{
// System.out.println(alpha++);
InterfaceView.statusMessageLabel.setText("Checking inbound messages");
try {
// System.out.println("Checking inbound messages");
readMessages();
} catch (FileNotFoundException ex) {
Logger.getLogger(Implementation.class.getName()).log(Level.SEVERE, null, ex);
}
InterfaceView.statusMessageLabel.setText("Processing messages");
// System.out.println("Checking outbound messages");
// processMessages();
InterfaceView.statusMessageLabel.setText("Checking outbound messages");
// System.out.println("Checking outbound messages");
// sendMessages();
InterfaceView.statusMessageLabel.setText("Waiting ... ");
try {
readMessages();
} catch (FileNotFoundException ex) {
Logger.getLogger(Implementation.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
t.sleep(5000);
} catch (InterruptedException e) {System.out.println("Thread Pause Exception");}
}
}
static class QueueSendingNotification implements org.smslib.IOutboundMessageNotification
{
public void process(String gtwId, OutboundMessage msg)
{
try {
System.out.println("Sent Message from: " + gtwId + " , " + msg.getId());
queuedMessages.remove(msg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void readMessages() throws FileNotFoundException
{
int check_Lenght = 0;
if (srv.getServiceStatus() == Service.ServiceStatus.STARTED)
{
try{
System.out.print ("\n\n\n\n\n" + "in the print block of above the interfaceview declaration");
InterfaceView check1 = new InterfaceView(SingleFrameApplication);
InboundMessage msg = null;
java.util.LinkedList<InboundMessage> list = new java.util.LinkedList<InboundMessage>();
srv.readMessages(list, InboundMessage.MessageClasses.UNREAD);
for (int i = 0; i < list.size(); i++)
{
msg = list.get(i);
//TODO: submit the incoming messages to the database from this point.
System.out.println("New incoming message from: " + msg.getOriginator() +" : \t" + msg.getText() + "\n");
saveSMS(msg.getOriginator(),msg.getText());
messageFor_File = msg.getText();
// Concatination of data
if (! messageFor_File.equals("Y")) {
Data_For_File = messageFor_File + Data_For_File ;
check_Lenght = Data_For_File.length();
System.out.print("The Value for Data For File" + messageFor_File );
if (check_Lenght >= check){
System.out.println("\n\n\n" + "I am in x " );
DataPacketing call = new DataPacketing();
call.Write_To_File(Data_For_File);
}
//put the message into database.
srv.deleteMessage(msg);
}
else
{
messageFor_File1 = messageFor_File;
srv.deleteMessage(msg);
Read_Message_Notification ();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
private void saveSMS(String originator, String text)
{
try
{
} catch (Exception e) {
e.printStackTrace();
}
}
public void processOutMessage(String sms)
{
try
{
// outMsg = new OutboundMessage ("03456305252", sms);
// outMsg = new OutboundMessage ("03219409778", sms);
outMsg = new OutboundMessage ("03456305252", sms);
outMsg.setId("1");
queuedMessages.add(outMsg);
srv.queueMessage(outMsg);
msg=msg+1;
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String Read_Message_Notification () throws TimeoutException {
// int check_Lenght;
//String messageFor_File1 = "";
System.out.println(messageFor_File1);
return messageFor_File1;
}
}
我的InterfaceView类的代码是
/*
* InterfaceView.java
*/
package smsapp;
import java.io.IOException;
import java.util.logging.Level;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import org.smslib.InboundMessage;
import org.smslib.TimeoutException;
import org.smslib.helper.*;
/**
* The application's main frame.
*/
public class InterfaceView extends FrameView {
private final String s = null;
public String fileName;
public String filePath;
public float fileLength;
public int No_Of_SMS;
public String check = "N";
public DataPacketing [] Packets_Array = null;
public DataPacketing start = new DataPacketing ();
public SingleFrameApplication app1 = new SingleFrameApplication ()
{
@Override
protected void startup() {
throw new UnsupportedOperationException("Not supported yet.");
}
};
private void customizations()
{
private void botton_startServiceMouseClicked(java.awt.event.MouseEvent evt) {
if (!this.botton_startService.isEnabled())
return;
javax.swing.JInternalFrame j = new javax.swing.JInternalFrame();
JOptionPane.showMessageDialog(j ,
"Please wait while the service is being connected.",
"Starting Server!",
JOptionPane.PLAIN_MESSAGE);
if (instance.startService()) {
j = new javax.swing.JInternalFrame();
JOptionPane.showMessageDialog(j ,
"Service Started Successfully.",
"Service Started",
JOptionPane.INFORMATION_MESSAGE);
this.botton_startService.setEnabled(false);
this.botton_connectPhone.setEnabled(false);
this.botton_stopService.setEnabled(true);
} else {
j = new javax.swing.JInternalFrame();
JOptionPane.showMessageDialog(j ,
"Service could not be started. Please connect the phone and try again.",
"Service Start Error",
JOptionPane.ERROR_MESSAGE);
this.botton_connectPhone.setEnabled(true);
}
}
private void botton_connectPhoneMouseClicked(java.awt.event.MouseEvent evt) {
if (!this.botton_connectPhone.isEnabled())
return;
if (!(instance.connectPhone(this.comPorts.getSelectedItem().toString()))) {
javax.swing.JInternalFrame j = new javax.swing.JInternalFrame();
JOptionPane.showMessageDialog(j ,
"Phone could not be connected. Please check the ports and try again.",
"Phone Connection Error",
JOptionPane.ERROR_MESSAGE);
}
//this.botton_connectPhone.setEnabled(false);
}
private void formPropertyChange(java.beans.PropertyChangeEvent evt) {
if (!(instance.connectPhone(instance.defaultPort) && (instance.startService())))
{
this.botton_connectPhone.setEnabled(true);
this.botton_startService.setEnabled(true);
this.botton_stopService.setEnabled(false);
}
}
private void SelectFileActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
fileName = f.getName();
filePath = f.getPath();
fileLength = f.length();
// System.out.println("You have chosen: " + fileName+"\n from the path: "+filePath+"\n of the length: "+fileLength+" KB");
//------------------------------------- Calculating the Number Of SMS Required from the File Size
float temp = fileLength/120;
No_Of_SMS = (int) Math.ceil(temp);
String SMS = " "+No_Of_SMS+" SMS";
SmsRequired.setText(SMS);
String size2 = " "+fileLength+" KB";
FileName.setText(fileName);
FileSize.setText(size2);
this.FileName.setVisible(true);
this.FileSize.setVisible(true);
this.SmsRequired.setVisible(true);
this.jLabel3.setVisible(true);
this.jLabel4.setVisible(true);
this.jLabel6.setVisible(true);
DataPackets.setText(" InterKnot:\\"+filePath);
try {
Packets_Array = start.Driver(fileName,filePath,fileLength,No_Of_SMS);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(InterfaceView.class.getName()).log(Level.SEVERE, null, ex);
}
this.DataPackets.setVisible(true);
// DataPacketing setFile = new DataPacketing();
//setFile.File_Name_Path(fileName, filePath);
}
private void FileNameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void FileNameKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void ReadandSendFileActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// DataPacketing [] Packets_Array = null;
// DataPacketing start = new DataPacketing ();
System.out.println("I am not yet in if");
String s = null;
if(check.equals("N")){
System.out.println("Do i Come here ????");
try {
//InboundMessage userNo = null;
//String info = "User: " + "wants to share file :"+ FileName+ " of size: "+FileSize+ "with you.To allow Reply Y";
String newInfo = "User Wants to share a file with you. To allow reply Y";
System.out.println("\n\n\n\n\n" + newInfo);
message1.processOutMessage(newInfo);
System.out.print("\n\n\n\n\n\n" + "I am In try to check for the read notification message");
}catch (Throwable t) {
System.out.println("The Exception" + t);
}
Implementation check1 = new Implementation ();
try {
check = check1.Read_Message_Notification();
} catch (TimeoutException ex) {
java.util.logging.Logger.getLogger(InterfaceView.class.getName()).log(Level.SEVERE, null, ex);
}
if (check.equals("Y")){
// --------------------------------------------converting String Array into String
StringBuffer result = new StringBuffer();
// -------------------------------------------- LOOP to send WHOLE file
for(int i=0; i<Packets_Array.length; i++)
{
String s1 = Packets_Array[i].Data_Segment;
System.out.println(s1);
message1.processOutMessage(s1);
}
// -------------------------------------------- To send First Packet of file
}
}
else
{
System.out.println("The User Refused to Allow the Transfer");
}
}
private void FileSizeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void FileSizeKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void SmsRequiredActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void SmsRequiredKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void DataPacketsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void DataPacketsKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JTextField DataPackets;
private javax.swing.JTextField FileName;
private javax.swing.JTextField FileSize;
private javax.swing.JLabel PicGlobe;
private javax.swing.JButton ReadandSendFile;
private javax.swing.JButton SelectFile;
private javax.swing.JTextField SmsRequired;
private javax.swing.JButton botton_connectPhone;
private javax.swing.JButton botton_startService;
private javax.swing.JButton botton_stopService;
public static javax.swing.JComboBox comPorts;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel5;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
public static javax.swing.JLabel statusMessageLabel;
public static javax.swing.JLabel statusMessageLabel1;
private javax.swing.JPanel tab_settings;
// End of variables declaration
Object table_keywords_columns[] = { "Keyword", "Reply Text"};
Object table_outgoingSMS_columns[] = { "Number", "Text", "Time"};
Object table_incomingSMS_columns[] = { "Number", "Text", "Time", "Auto-Reply Keyword"};
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private Implementation instance;
private JDialog aboutBox;
public Implementation message1 = new Implementation();
JProgressBar progressBar;
void messagecheck(){
System.out.print ("\n\n\n\n\n\n\n" + "I am in the message code of the interfaceview class");
}
}
该怎么办 ?
最佳答案
您的第一个示例失败,因为您没有为变量分配任何内容,因此它是null
,您会得到一个NullPointerException
。
您的第二个示例失败,因为构造函数需要参数,而您没有提供参数。您需要查看interfaceview
类的构造函数并提供相关的参数。
例如,如果interfaceview
的唯一构造函数如下所示:
public interfaceview(String foo) {
// ...
}
...然后创建它,您必须提供一个
String
:interfaceview check1 = new interfaceview("Some string here");
我认为退后一步并完成一些Java教程可能会很有用(那里不乏它们)。同样,就其价值而言,通常在Java接口和类中都使用最初具有上限的camelCase编写,例如
InterfaceView
而不是interfaceview
。如果愿意,可以忽略该约定,但是遵循该约定可使其他代码更清晰。关于java - 构造函数问题:实际列表和正式列表的长度不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10427846/