Welcome to Dream.In.Code
Become a Java Expert!

Join 150,423 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,091 people online right now. Registration is fast and FREE... Join Now!




Need help with error -- non-static method cannot be referenced from a

 
Reply to this topicStart new topic

Need help with error -- non-static method cannot be referenced from a

HLC11
19 Mar, 2008 - 05:20 PM
Post #1

New D.I.C Head
*

Joined: 19 Mar, 2008
Posts: 10


My Contributions
Hi everyone, I am taking a java course and this is my second project for that class. For this project we are supposed to create 2 class files and a main project file. A Room class, a RoomSorting class and Project2 main file. This program takes a text file with classroom information and creates a array of objects and then sorts it by room capacity and then prints out to a JFrame with two text areas, one shows the unsorted list and the other shows the sorted list. The text file format is building name|room number| room capacity. I have tested the Room class file with test programs from my professor and it works fine, but I am having trouble with the RoomSorting class file. I have not yet tested the sorting since I can't get the compareTo method to work. I'll appreciate any and all suggestions on how to fix this error. The error i get is :

RoomSorting.java:21: non-static method compareTo(Room) cannot be referenced from a static context
if (Room.compareTo(room) < 0 ) {
^
1 error

I have checked over the web on many different sites and came across others saying you have to instatiate an instance of that class but only if it the constructor has no arguments but the assignment I have has to have arguments as it was stated by the professor. I have tried many other ways but didn't work so i figured I would post and ask for help as this is the last part for me to finish my assignment.

These are the parts of the java files.

CODE
//Room.java
import javax.swing.*;
import java.util.*;

public class Room {

private String building, roomNumber;
private int capacity;



   public Room (String building, String roomNumber, int capacity) {

      this.building = building;
          this.roomNumber = roomNumber;
          this.capacity = capacity;

   } // Room




   public int compareTo (Room other) {

      if (capacity < other.capacity)
         return(-1);
      else if (capacity == other.capacity)
         return(0);
      else
         return(1);

   } // compareTo
  


} // class Room

CODE
//Project2.java

import javax.swing.*;
import java.awt.*;
import java.util.*;


public class Project2 extends JFrame {

   final static int MAX_ROOMS = 100;
   static int arrayLength;
   static Room [] roomsArray = new Room [MAX_ROOMS];

   private JTextArea textAreaOriginal;
   private JTextArea textAreaSorted;
   private JTextField messageField;



   public Project2() {

      setSize (450, 300);
      setLocation (100, 100);
      setTitle ("Sorts file of rooms by capacity");

      Container contentPane = getContentPane();

      JPanel panel = new JPanel();
      panel.setLayout (new GridLayout (1, 2));
      contentPane.add(panel, BorderLayout.CENTER);

      textAreaOriginal = new JTextArea();
      textAreaOriginal.setEditable(false);
      panel.add(new JScrollPane(textAreaOriginal));

      textAreaSorted = new JTextArea();
      textAreaSorted.setEditable(false);
      panel.add(new JScrollPane(textAreaSorted));

      messageField = new JTextField();
      messageField.setEditable(false);
      contentPane.add(messageField, BorderLayout.SOUTH);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);

   } // Project2 window


   public static void main (String args[]) {

      if (args.length !=1) {
         System.out.println ("\nYou must specify a filename!");
         System.exit(0);
      } // if


         final String inputFileName = args[0];
         arrayLength = readFile (inputFileName, roomsArray);
         Project2 window = new Project2();
        // RoomSorting.sortByCapacity(roomsArray,arrayLength);
   } // main method


   public static int readFile (String filename, Room[] rooms) {

    .....................


   } // readFile


   public static void displayResults (JTextArea area, Room[] rooms, int length) {

    ..............................
  
   } // displayResults

} // Project2

CODE
//RoomSorting.java

import javax.swing.*;
import java.util.*;

public class RoomSorting {

   public void sortByCapacity (Room[] rooms, int length) {

      for (int lengthFilled = 0; lengthFilled < length -1; lengthFilled++) {
         Room room = rooms[lengthFilled];
         int indexLow = lengthFilled;

            for (int j = lengthFilled +1; j <= length-1; j++) {
               if (Room.compareTo(room) < 0 ) {
                  indexLow = j;
               } // if
            } // for

         Room temp1 = rooms[indexLow];
         rooms[indexLow] = rooms[lengthFilled];
         rooms[lengthFilled] = temp1;

      } // for

   }  // method sortByCapacity

} // class RoomSorting


I am sorry for posting all this code but I figured it would be easier for someone looking at it to understand the purpose of the program.
Thanks,
HLC

This post has been edited by HLC11: 19 Mar, 2008 - 10:55 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Need Help With Error -- Non-static Method Cannot Be Referenced From A
19 Mar, 2008 - 05:36 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,291



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
The compareTo method of class Room is non static. That means it belongs to objects create from that class, or instances of the class. Consider how your compareTo method works:

CODE

public int compareTo (Room other) {
   if (this.capacity == other.capacity) { return 0; }
   return (this.capacity < other.capacity) ? -1 : 1;
}


See the "this". Class "Room" has no this. What can the method possibly be thinking? The point of the method is to compare the current Room object to the other Room object, right?

Perhaps you're trying to do this:
CODE

if (rooms[j].compareTo(rooms[indexLow]) < 0 ) {
   indexLow = j;
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

HLC11
RE: Need Help With Error -- Non-static Method Cannot Be Referenced From A
19 Mar, 2008 - 07:06 PM
Post #3

New D.I.C Head
*

Joined: 19 Mar, 2008
Posts: 10


My Contributions
QUOTE(baavgai @ 19 Mar, 2008 - 06:36 PM) *

The compareTo method of class Room is non static. That means it belongs to objects create from that class, or instances of the class. Consider how your compareTo method works:

CODE

public int compareTo (Room other) {
   if (this.capacity == other.capacity) { return 0; }
   return (this.capacity < other.capacity) ? -1 : 1;
}


See the "this". Class "Room" has no this. What can the method possibly be thinking? The point of the method is to compare the current Room object to the other Room object, right?

Perhaps you're trying to do this:
CODE

if (rooms[j].compareTo(rooms[indexLow]) < 0 ) {
   indexLow = j;
}


Hope this helps.


Thanks baavgai for you help, I really appreciate it. That fixed the problem. This project is building up on the previous which had us output to JOptionPane's and since it builds on the previous you can use some of the same code with some modifications, I didn't even think of the way you told me because I was thinking that since it was a Room object that was being compared by capacity and that method was in the Room class that I needed to add the Class name before the method.


This post has been edited by HLC11: 19 Mar, 2008 - 08:24 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month