PDA

View Full Version : ارسال ارگومان



yegane8
پنج شنبه 22 تیر 1391, 22:28 عصر
سلام
من می خوام از متد ActionListener در یک کلاس یک string به یک متد دیگر در کلاس دیگر بفرستم
اینطوری نوشتم :
public String question;
public String choice;
private void toefl1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
question =null;
question =evt.getActionCommand();
choice = question+"choice" ;

SwingRadioButton test1 = null;
try {
test1 = new SwingRadioButton();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}

test1.setChoiceFile(choice);
test1.setQuestionFile(question);
حالا تو اون یکی کلاس اینطوری نوشتم

private String choiceFile;
private String questionFile;

public void setChoiceFile(String choice){

this.choiceFile =choice;

}
public String getChoiceFile(){

return choiceFile;

}

public void setQuestionFile(String question){

this.questionFile =question;

}
public String getQuestionFile(){

return questionFile;

}
توی یک متد اینطوری ازشون استفاده کردم
Scanner input1 = new Scanner(new File(this.getChoiceFile()));
Scanner input2= new Scanner(new File(this.getQuestionFile()));


اما این خطا رو میده :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.File.<init>(File.java:251)
at SwingRadioButton.testFrame(SwingRadioButton.java:5 5)
at SwingRadioButton.<init>(SwingRadioButton.java:33)
at NewJFrame.toefl1ActionPerformed
فکر کنم اصلاstring مقدار دهی نمیشه
باید چی کار کنم؟
باتشکر

spiderman200700
پنج شنبه 22 تیر 1391, 23:15 عصر
مشکل از کلاس SwingRadioButton هست.
این کلاس ساخته نمیشه.
احتمالا مشکل داخل سازنده ی کلاس هست.
موقع خوندن فایلی چیزی
به هر حال چون کدهای SwingRadioButton اینجا نیست نمیشه خوب نظر داد

yegane8
پنج شنبه 22 تیر 1391, 23:48 عصر
قبلا که ورودی ها یه فایل ثابت بودن مشکلی نداشت
اینطوری
("name.txt")Scanner input1 = new Scanner(new File(()));
به نظرتون بازم مشکل از سازنده است؟

yegane8
جمعه 23 تیر 1391, 01:30 صبح
constructor رو اینطوری کردم
protected SwingRadioButton(String ch,String q) throws FileNotFoundException {

super("SwingRadioButton");
this.choiceFile = ch;
this.questionFile =q;

بخش main همین کلاس رو هم اینطوری

public static void main(String args[]) throws FileNotFoundException {
SwingRadioButton swingRadioButton = new SwingRadioButton(choiceFile,questionFile);
}
بخش ActionListener تو اوون یکی کلاس رو هم اینطوری

private void toefl1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
question =null;
question =evt.getActionCommand();
choice = question+"choice" ;

try {
SwingRadioButton test1 = new SwingRadioButton(question,choice);
test1.showTest();
} catch (Exception ex) {
ex.printStackTrace();
}
اما بازم نشد :((((
خروجی :
java.io.FileNotFoundException: toefl (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:128)
at java.util.Scanner.<init>(Scanner.java:656)
at SwingRadioButton.testFrame(SwingRadioButton.java:6 1)
at SwingRadioButton.<init>(SwingRadioButton.java:36)
at NewJFrame.toefl1ActionPerformed(NewJFrame.java:321 )
at NewJFrame.access$900(NewJFrame.java:22)
at NewJFrame$10.actionPerformed(NewJFrame.java:182)
البته ادامه هم دارد تو خروجی نوشته نمی تونه فایل موردنظر رو پیدا کنه اما مطمءنم که هست
چی کار کنم ؟
باتشکر

yegane8
جمعه 23 تیر 1391, 03:01 صبح
هر کاری می کنم خروجی اینه
java.lang.NullPointerException
at java.io.File.<init>(File.java:251)
at SwingRadioButton.testFrame(SwingRadioButton.java:5 8)


:((((

spiderman200700
جمعه 23 تیر 1391, 12:25 عصر
اینجوری نمیشه فهمید باید کل کد رو دید، بعد نظر داد