java - JFrame always prints out null for variables inside action listener -
the following program's goal ask user input resistor value, of program output corresponding colors each digit. not include digits. however, program done, i've made attempt incorporate jframe thing, except hung on how print corresponding colors in action listener.
i asked question however, got limited replies due forgetting enter specific tags. however, user answer use arraystostring did practicly nothing, since program still managed print null.
cannot refer non variable inside action listener (jframe) methods below jframe sopposed gather information each color band of resistor depending on digit number, in action listener try , print out colors, rather prints null (3 times)
i have tried viewing various tutorials online, , java api , guidelines, none of help. in general seem unaware of how incorporate code written jframe, whether it's tedious process, willing corporate , grateful insight on how tackle predicament.
import java.io.*; import javax.swing.*; //import javax.swing.jframe; //import javax.swing.jlabel; //import javax.swing.jbutton; //import javax.swing.jpanel; //import javax.swing.jtextfield; import java.awt.event.*; import java.util.arrays; public class test extends jframe { public static void main (string [] args) throws ioexception { bufferedreader myinput = new bufferedreader (new inputstreamreader (system.in)); //calling variables string input; int numinput; jlabel l = new jlabel("hello , welcome program (press button start instructions"); //l.setalignmentx(0); // l.setalignmenty(0); //calling arrays int [] array = new int [5]; int [] array2 = new int [3]; string [] array3 = new string [3]; string[] colours = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"}; jframe f = new jframe("hello jframe"); f.setsize(500,500); //f.getcontentpane().setbackground(color.cyan); f.add(l); f.setdefaultcloseoperation(jframe.exit_on_close); f.setvisible(true); //jtextfield t = new jtextfield(16); jpanel p = new jpanel (); jbutton b = new jbutton("press me") ; // b.setalignmentx(0); // b.setalignmenty(0); b.addactionlistener(new actionlistener(){ public void actionperformed (actionevent e) { joptionpane.showmessagedialog(null,"in following program (the user!) input number of resistor value \nthe program pass information methods , proceed print out \nthe coorelating colors (press button asked input)"); int number = integer.parseint(joptionpane.showinputdialog("please enter resistor value")); final string [] array3 = new string [3]; joptionpane.showmessagedialog(null, "the colors : " + (arrays.tostring(array3))); } }); p.add(b); p.add(l); //p.add(t); f.add(p); system.out.println("hello , welcome program (press key con't)"); input = myinput.readline (); system.out.println("in following program (the user!) input number of resistor value"); system.out.println("the program pass information methods , proceed print out"); system.out.println("the coorelating colors (press key asked input)"); input = myinput.readline(); system.out.println("enter resistor value (note resistors can acount 4 decimal places"); input = myinput.readline (); numinput = integer.parseint (input); //colours values array2 = values(array, input, colours); for(int = 0 ; < 3; i++){ array3[i] = digitcolours(array2[i], colours); system.out.println(array3[i]);// prints colours values } //prints 4th colour multiplier system.out.println(decimalplaces(input, colours)); } public static int[] values (int [] digit, string num, string[] colours) { string holder; double numholder; int lengthofinput; int holder2; //tollerance holder = num.substring(3,4); digit[3] = integer.parseint(holder); holder2 = integer.parseint(num); // checks see if above 5 if(digit[3] < 5){ digit[3] = digit[3]/holder2 * 100; } else if(digit[3] > 5){ digit[3] = 10 - digit[3]; digit[3] = digit[3]/holder2 * 100; } system.out.println(digit[3]); //rounding of input lengthofinput = num.length() - 3; numholder = double.parsedouble(num); numholder = numholder/(math.pow(10,lengthofinput)); numholder = (int)(math.round(numholder)+0.5); // first 3 digits for(int = 0; < 3; i++){ holder = num.substring(i,i+1); digit[i] = integer.parseint(holder); } //print out information /*system.out.println("the first digit rounded to:" + (int)digit[0]); system.out.println("the second digit roudned to:" + (int)digit[1]); system.out.println("the third digit roudned to:" + (int)digit[2]); */ /* */ return new int[] {digit[0], digit[1],digit[2],digit[3]} ;// return } public static string digitcolours(int decimalplace, string[] colours){ //calling additional variables string answer; answer = colours[decimalplace]; return answer; } //method find multiplier public static string decimalplaces(string input, string[] colours){ //calling additional variables int length = input.length(); string answer; length = length - 3; answer = colours[length]; return answer; } }
the array, array3, null since has been declared way:
final string[] array3 = new string[3]; joptionpane.showmessagedialog(null, "the colors : " + (arrays.tostring(array3)));
as can see above code, has never been assigned string objects of kind , defaults null. must fill first hold non-null values. think of object array similar egg crate: unless fill eggs, empty.
i recommend make variables want passed inner class, final. must final in outer class, not in inner class:
more importantly, i'd of code out of main method, out of static-land , instance-land belongs. can use class fields , not have worry local final variables. think major weakness of current program -- static code , not enough oop-compliant classes , objects.
your other problem appear trying mix swing gui console program gets user input both gui , standard in via bufferedreader, , can lead disaster. urge chose 1 or other, better swing gui in opinion , not try mix these unmixable things.
Comments
Post a Comment