0

以下のコードでは、jButton1ActionPerformedメソッドからアクセスしたときに変数uNombaとlistの値がNULLである理由がわかりません。「newNewPlayer(uNomba、count、check、list).load();」を正常に実行する方法について、ご協力いただければ幸いです。すべての値がNewPlayerクラスに渡されるようにします。ありがとうございました。

最初のクラス-つまり、NewPlayerクラス

package mysound;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class NewPlayer extends JPanel implements KeyListener, Runnable{
boolean isUpPressed, isDownPressed, isSpacePressed, isDone;
static JFrame f;
int spacebars=0;
boolean within;
public List spacebarLogMs = new ArrayList(); 
public List numSbar = new ArrayList();
Calendar cal = Calendar.getInstance();
LogResult logNow = new LogResult();
String directory;
String tabname; //table name used in the database connection
String bdir;
private int uNomba; //user number obtained from NewSound class
private String target;
private int incr;
private int userno;
private boolean moveon=true;
private List randlist;
private List numlist;

public void load() {
    f = new JFrame();
    f.setSize(600,300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(this);
    f.setVisible(true);     
    setFocusable(true);
    addKeyListener(this);
    new Thread(this).start();
}

public NewPlayer() {   

}

public NewPlayer(int UNOMBA, List NUMLIST){
    this.uNomba = UNOMBA; //user number obtained from NewSound class
    this.numlist=NUMLIST;
}    
public NewPlayer(int USERNO, int INCR, boolean MOVEON, List NUMLIST){
    this.userno=USERNO;
    this.incr=INCR;
    this.moveon=MOVEON;
    this.numlist=NUMLIST;
}

public void keyTyped(KeyEvent ke) {
}

public void keyPressed(KeyEvent ke) {
    switch(ke.getKeyCode()) {
        case KeyEvent.VK_UP: isUpPressed = true; break;
        case KeyEvent.VK_DOWN: isDownPressed = true; break;
        case KeyEvent.VK_SPACE: isSpacePressed = true; 

        numSbar.add(System.currentTimeMillis());
            System.out.println("That was a spacebar. "+spacebars++);
            System.out.println("Current time: "+numSbar);
            break;
    }
}

public void keyReleased(KeyEvent ke) {
    switch(ke.getKeyCode()) {
        case KeyEvent.VK_UP: isUpPressed = false; break;
        case KeyEvent.VK_DOWN: isDownPressed = false; break;
        case KeyEvent.VK_SPACE: isSpacePressed = false; break;
    }
}

public void closePrj(){
    f.dispose();
}

public void run() { //introduce a target sound

   String targetChoice;
   int tIndex;       
   int i;
   bdir="C:\\Users\\Abiodun\\Desktop\\testdata\\main\\zero\\atext\\"; //dir for text files


   MainPlayer items =  new MainPlayer (uNomba);

   i=incr;
   while(moveon){
       System.out.println("Counter i: "+i+" Numlist: "+numlist);
       if (i<numlist.size()){
            int num = (int) numlist.get(i);
            System.out.println("Num :"+num);
            items.selectTarget(num);           
            items.selectChallenge(num);
            items.playChallenge();
            new WriteTime(bdir).tagTime(numSbar);
            items.dataLogger();
            moveon=false;
            new Continue (uNomba, i, moveon, numlist).load();
       }

   }
}
}

2番目のクラス、つまり、Continueクラス

public class Continue extends javax.swing.JDialog {
    private int count;
    private int usernumb;
    private boolean check;
    private int uNomba;
    private String cdirectory;
    private String cbdir;
    private String ctabname;
    private String ctarget;
    private List list;

/**
 * Creates new form Continue
 */
public Continue(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
}

public Continue(int CUNOMBA, int COUNT, boolean CHECK, List NLIST){
    this.uNomba = CUNOMBA; //user number obtained from NewSound class
    this.count=COUNT;
    this.check=CHECK;
    this.list=NLIST;
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    new NewPlayer().setVisible(false);//closePrj();
    count++;
    check=true;
    new NewPlayer(uNomba, count, check, list).load();        
            System.out.println("Continue: UserNumber: "+uNumb+", Count: "+count+", Check: "+check+", nList"+lst);
    this.setVisible(false);         
}

sgrohに感謝します。追加したものは次のとおりです。InNewPlayerクラスで次を作成しました。Continuect=new Continue(new NewPlayer(uNomba、i、moveon、numlist));

継続クラスでは、プライベートNewPlayer np;

public Continue (NewPlayer npy){
    this.npy=np;
}

要約すると、私が抱えている主な問題は、ContinueクラスからNewPlayerクラスから渡した値にアクセスできないことです。私はContinueクラスで次のコンストラクターの側で値をテストしましたが、Continueクラスの他の場所ではテストしませんでした。

public Continue(int CUNOMBA, int COUNT, boolean CHECK, List NLIST){
    this.uNomba = CUNOMBA; //user number obtained from NewSound class
    this.count=COUNT;
    this.check=CHECK;
    this.nlist=NLIST;


   System.out.println("Continue-constructor - uNomba: "+uNomba+", nList: "+list); //works fine! but not outside this constructor.
}
4

2 に答える 2

0

このコードはコンパイルもされます。コンストラクターのデフォルトはありません(フィールドなし)。これ:

public Continue(java.awt.Frame parent, boolean modal) {

この:

public Continue(int CUNOMBA, int COUNT, boolean CHECK, List NLIST){

これはコンパイルされません:

Continue ctn = new Continue();
于 2012-10-03T19:38:16.567 に答える
0

適切なコンストラクターを使用して Continue オブジェクトを作成するか、既定のコンストラクターを作成する必要があります。

また、存在しない System.out.println の変数 uNumb を出力したいとします。

于 2012-10-03T19:42:05.733 に答える