Results 1 to 6 of 6
- 02-03-2009, 11:48 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 25
- Rep Power
- 0
Debuggin help - simple program non-static method errors
below is a working program:
The code above contains changes I made to the original program I made (I got from a book) below:Java Code:import javax.swing.JTextArea; import javax.swing.JOptionPane; public class MatrixManipulation { public static void main(String args[]) { JTextArea outputArea = new JTextArea(); int array1[][] = {{1,2,3}, {4,5,6}}; int array2[][] = {{1,2}, {3}, {4,5,6}}; outputArea.setText("Values in array1 by row are\n"); buildOutput(array1, outputArea); outputArea.append("\nValues in array2 y row are\n"); buildOutput(array2, outputArea); JOptionPane.showMessageDialog(null, outputArea, "Matrix Manipulation", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } public static void buildOutput(int array[][], JTextArea outputArea) { // loop through the array's rows for (int row=0; row<array.length; row++) { // loop through the columns for (int column=0; column<array[row].length; column++) { outputArea.append(array[row][column] + " "); } outputArea.append("\n"); } } }
I want to keep the JtextArea as global so that other function have global access to it. However with the code above this error exists:Java Code:import javax.swing.JTextArea; import javax.swing.JOptionPane; public class MatrixManipulation { JTextArea outputArea; public static void main(String args[]) { outputArea = new JTextArea(); int array1[][] = {{1,2,3}, {4,5,6}}; int array2[][] = {{1,2}, {3}, {4,5,6}}; outputArea.setText("Values in array1 by row are\n"); buildOutput(array1); outputArea.append("\nValues in array2 y row are\n"); buildOutput(array2); JOptionPane.showMessageDialog(null, outputArea, "Matrix Manipulation", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } public void buildOutput(int array[][]) { // loop through the array's rows for (int row=0; row<array.length; row++) { // loop through the columns for (int column=0; column<array[row].length; column++) { outputArea.append(array[row][column] + " "); } outputArea.append("\n"); } } }
"non-static variable outputArea cannot be referenced from a static context"
Also when I call the function buildOutput I get this error:
"non-static method buildOutput(int[][]) cannot be referenced from a static context"
I Know like I said the first program works but I really want to find out what is wrong with the second one so that I familiarize myself with Java syntax.
Any help is appreciated! Thanks.
-
It will work if you make everything static including your outputArea variable and your buildOutput method. Having said that, don't get used to this. As you progress in Java, you will learn to avoid using static variables and static methods if at all possible as you can't use OOPs techniques with this.
ps: Here's to hoping that you'll do better with this post and respond to anyone who answers your questions. You didn't do this with your previous post. Good luck.
- 02-04-2009, 12:51 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 25
- Rep Power
- 0
Ok thanks for the reply. Well I might not have replied if someone answered a question because I really don't like to post more comments and clutter stuff up if there really is no need to! And I will keep doing that same thing to keep things cleaner. But thanks for your advice.
However, here I'm still at a loss. I understand that I have to use "static". But you say I shouldn't use static. So how do I make the code in the second block work? This is code I got from a book but doesn't seem to work. Thanks!
- 02-04-2009, 01:01 AM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
outputarea is an instance variable owned by a specific instance or object of your type, that is it isn't static and owned by the class. To access it, you need an instance by using the new operator and access that instance's outputarea:
and you access it via mm.outputarea, however it isn't good practice to access the fields of an object directly but to rather create methods to do this for you.Java Code:MatrixManipulation mm = new MatrixManipulation();
- 02-04-2009, 01:14 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 25
- Rep Power
- 0
So in other words I programmed it wrong lol. Ok...will organize my code thanks.
-
a simple thanks is always appreciated and never clutter.Well I might not have replied if someone answered a question because I really don't like to post more comments and clutter stuff up if there really is no need to! And I will keep doing that same thing to keep things cleaner.
Anyway, if I were doing this myself, I'd do what nugget suggests, try to code this as methods of a class, then in the main instantiate an object of the class and call the appropriate methods on the object. My guess is that you're quite new at this, and if so, a decent introduction to Java book or the Sun intro tutorials should help you out a lot here. Best of luck.
Similar Threads
-
Help with Errors in Inventory Program
By ljk8950 in forum AWT / SwingReplies: 3Last Post: 08-08-2008, 11:49 PM -
Static variable context Errors ?
By Shyam Singh in forum New To JavaReplies: 16Last Post: 08-08-2008, 09:11 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM -
Non-Static method in static context error
By wizmang in forum New To JavaReplies: 4Last Post: 04-24-2008, 08:51 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks