Results 1 to 6 of 6
  1. #1
    RR_QQ is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default Debuggin help - simple program non-static method errors

    below is a working program:

    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");
            }
        }
    
    }
    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
    {
        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");
            }
        }
    
    }
    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:

    "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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    RR_QQ is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    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!

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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:

    Java Code:
    MatrixManipulation mm = new MatrixManipulation();
    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.

  5. #5
    RR_QQ is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    So in other words I programmed it wrong lol. Ok...will organize my code thanks.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.
    a simple thanks is always appreciated and never clutter.

    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

  1. Help with Errors in Inventory Program
    By ljk8950 in forum AWT / Swing
    Replies: 3
    Last Post: 08-08-2008, 11:49 PM
  2. Static variable context Errors ?
    By Shyam Singh in forum New To Java
    Replies: 16
    Last Post: 08-08-2008, 09:11 PM
  3. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  4. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •