问题描述:

In the code below, I don't know why the values of variables uNomba and list are NULL when accessed from jButton1ActionPerformed method. I would appreciate your help, on how I can successfully execute "new NewPlayer(uNomba, count, check, list).load();" such that all the values are passed to NewPlayer class. Thank you.

The first class - i.e The NewPlayer class

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();

}

}

}

}

The second class i.e the Continue class

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);

}

Thanks sgroh. Here is what I just added: I created the following in

In NewPlayer class:

Continue ct = new Continue (new NewPlayer(uNomba, i, moveon, numlist));

In Continue Class,

private NewPlayer np;

public Continue (NewPlayer npy){

this.npy=np;

}

Just a recap, the main problem I am having is that I cannot access the values I passed from NewPlayer class from Continue class. I tested the values in side the following constructor in Continue class but not anywhere else in Continue class.

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.

}

网友答案:

This code even compile, You haven't a constructor default (without fields). this:

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

and This:

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

this woun't compile:

Continue ctn = new Continue();
网友答案:

You have to create the Continue object using the right constructor or create the Default constructor.

You want also to print the variable uNumb in the System.out.println that doesn't exists.

相关阅读:
Top