我不是很擅长OOP,我遇到了这个问题,构造函数没有返回任何东西。
public class CrossEntropy extends Thread{
protected CEMobileAgentMigrationPlan appOwner;
private SensorNode[] sensorNodes;
private JLabel status;
private JTextArea textArea;
Integer temp = null;
boolean first=true;
int tempor;
int populasi=1000;
double a = 0.8;
double g=(int)(populasi/3);
int max=appOwner.sensorCount+populasi;
private double costa=0;
private int[] samatidak;
public int[] mem;
private double[] sortbox;
private double w[][] = new double[max][max];
private double p[][] = new double[max][max];
private double[][] pn;
private int[][] jalur;
private double[] cost;
boolean first2;
public CrossEntropy(CEMobileAgentMigrationPlan appOwner){
System.out.println("hi !");
this.appOwner=appOwner;
System.out.println("hi hi hello !");
this.sensorNodes=appOwner.sensorNodeList;
System.out.println("hello !");
}
//method shortened
public void run(){//engga kepanggil
System.out.println("Testtament");
double time = System.currentTimeMillis();
double thisCost = 0.0;
double oldCost = 0.0;
int countSame=0;
int duration=0;
double hasilakhir =0;
boolean cek = false;
//matrikRate();
while (countSame<1000){
membangkitkanTour();
getCost();
thisCost = costa;
if (cek==false){
oldCost=thisCost;
cek=true;
}
if (thisCost<oldCost){
oldCost=thisCost;
costa=0;
getCost();
hasilakhir=costa;
countSame=0;
}else{
countSame++;
//appOwner.paint();
}
costa=0;
}
duration = (int) (System.currentTimeMillis()-time);
StringBuffer sb = new StringBuffer();
for (int i=0; i< appOwner.sensorCount; i++){
sb.append("Node Address: " + appOwner.sensorNodeList[mem[i]].getAddress());
sb.append(" RSSI: " + appOwner.sensorNodeList[mem[i]].getSignalStrength());
sb.append(" Battery Level: " + appOwner.sensorNodeList[mem[i]].getRemainingBattery());
sb.append("\n");
}
String status = "CE finds Solution after " + duration + " ms with max rating= " + hasilakhir;
String migrationInfo = sb.toString();
appOwner.setStatus(status, "Urutan Rute Migrasi: \n" + migrationInfo);
}
}
这是我想要调用构造函数的类
public class CEMobileAgentMigrationPlan extends JFrame {
//protected SensorNodePlacementMap map = null;
SensorNode[] sensorNodeList;
CrossEntropy worker;
private JTextArea textArea;
boolean started = false;
private JLabel status;
int sensorCount;
public CEMobileAgentMigrationPlan(){
getContentPane().setLayout(new BorderLayout());
status = new JLabel("Starting App...");
textArea = new JTextArea("Listening... \b");
getContentPane().add(status,BorderLayout.SOUTH);
getContentPane().add(textArea,BorderLayout.CENTER);
setSize(500,400);
setTitle("Cross Entropy for Mobile Agent Itinerary Planning");
this.setResizable(true);
setVisible(true);
}
public void setSensorNodes(SensorNode[] sensorNodes){
this.sensorNodeList = sensorNodes;
this.sensorCount = this.sensorNodeList.length;
}
public int getSensorCount(){
return sensorCount;
}
public SensorNode[] getSensorNodeList(){
return sensorNodeList;
}
public void setStatus(String statusTxt, String migrationInfo){
status.setText(statusTxt);
textArea.setText(migrationInfo);
}
public void setSensorNodeList(SensorNode[] sensorNodeList) {
this.sensorNodeList = sensorNodeList;
}
public void start(){ // yang ini tidak bisa
this.started=true;
//map.update(map.getGraphics());
//if (worker!= null)worker=null;
System.out.println("lol");
worker = new CrossEntropy(this); <<<<< this is the troublemaker
System.out.println("lol1111");
worker.setPriority(Thread.MIN_PRIORITY);
//map.update(map.getGraphics());
worker.start();
}
}
我已经做了一些类似的课程,找出原因,但我找不到:(
答案 0 :(得分:1)
如果您提供的代码示例已完成,那么在尝试构造对象时,您将获得NullPointerException。这条线
int max=appOwner.sensorCount+populasi;
会导致它,因为构造对象时appOwner为null。在调用构造函数之前初始化属性。你应该在construtor本身计算这个值,例如
int max;
public CrossEntropy(CEMobileAgentMigrationPlan appOwner){
System.out.println("hi !");
this.appOwner=appOwner;
max=appOwner.sensorCount+populasi;
System.out.println("hi hi hello !");
this.sensorNodes=appOwner.sensorNodeList;
System.out.println("hello !");
}