Hello

Everytime i compile this i keep getting an error on non-static method can not bu passed to static method, i understand that. But how can i get around this, without making the non-static method static , because it has variable relating to otha methods ... i also keep getting an array error for the variables userTime & i .

Can neone help me one this one please its very vital im almost done here..

Thanks For Ne Help Given In Advance

import java.util.Vector;
import java.io.*;


public class Schedule
{
		Vector ch1Time  = new Vector();
		Vector ch2Time  = new Vector();
		
	public static void main (String args[])
	{
		mainMenu();
		
	}
	

	public void mainMenu()
	{
		int choice1 = 0;
	
		do
		{
		System.out.println("************* CHANNEL ONE MENU *************");
		System.out.println("ENTER 1 TO ADD PROGRAMMES TO CHANNEL ONE");
		System.out.println("ENTER 2 TO REMOVE PROGRAMMES FROM CHANNEL ONE");
		System.out.println("ENTER 3 TO FIND EMPTY SPACE IN CHANNEL ONE");
		System.out.println("ENTER 4 TO ADD PROGRAMMES TO CHANNEL TWO");
		System.out.println("ENTER 5 TO REMOVE PROGRAMMES FROM CHANNEL TWO");
		System.out.println("ENTER 6 TO FIND EMPTY SPACE IN CHANNEL TWO");
		System.out.println("ENTER 0 TO EXIT MENU");
		
		
			try
			{
				BufferedReader reader;
				reader = new BufferedReader(new InputStreamReader(System.in));
				String i = reader.readLine();
				choice1 = Integer.parseInt(i);
				
	
				if(choice1==1)
				{
					addProgrammesCH1();
				}
			
				else if (choice1==2)
				{
					removeProgrammesCH1();			
				}
				
				else if (choice1==3)
				{
				//	checkCH1();			
				}
				
				else if(choice1==4)
				{
				//	addProgrammesCH2();
				}
			
				else if (choice1==5)
				{
				//	removeProgrammesCH2();			
				}
				
				else if (choice1==6)
				{
				//	checkCH2();			
				}
				
				else if (choice1==0)
				{
					System.exit(0);			
				}
					
			}
	
			catch (IOException e)
			{
				System.out.println(e);
			}
		
		}while(choice1!=0);
	}

	
	public void addProgrammesCH1()
	{
		int choice2 =0;
		int userTime;
		do				
		{

			System.out.println("********** ADD TO CHANNEL ONE **********");
			System.out.println("ENTER 1 TO CHOOSE A DAY AND TIME");
		//	System.out.println("ENTER 2 TO CHOOSE A TIME");
			System.out.println("ENTER 3 TO CHOOSE A PROGRAM");
			System.out.println("ENTER 4 TO GO TO MAIN MENU");
			System.out.println("ENTER 0 TO EXIT MENU");

			
			try
			{
				
				BufferedReader reader;
				reader = new BufferedReader(new InputStreamReader(System.in));
	
				choice2 = Integer.parseInt(reader.readLine());
				
		
				if(choice2==1)
				{
					schDay();
				}
			
			/*	else if (choice2==2)
				{
					schTime();			
				}
					*/			
				else if (choice2==3)
				{
			//		schProgram();
				}
				
				else if (choice2==4)
				{
					mainMenu();
				}
				
				else if (choice2==0)
				{
					System.exit(0);			
				}
			}
			catch (Exception e)
			{
				System.out.println(e);
			}
		
		}while(choice2!=0);	
	}
	
	public void schDay()
	{
		int choice2 =0;

		do				
		{

			System.out.println("********** ADD TO CHANNEL ONE **********");
			System.out.println("ENTER 1 TO ADD TO SUNDAY");
			System.out.println("ENTER 2 TO ADD TO SUNDAY");
			System.out.println("ENTER 3 TO ADD TO SUNDAY");
			System.out.println("ENTER 4 TO ADD TO SUNDAY");
			System.out.println("ENTER 5 TO ADD TO SUNDAY");
			System.out.println("ENTER 6 TO ADD TO SUNDAY");
			System.out.println("ENTER 7 TO ADD TO SUNDAY");
			System.out.println("ENTER 0 TO EXIT MENU");



		ch1Time.insertElementAt(" 6:00",0);
		ch1Time.insertElementAt(" 6:30",1);
		ch1Time.insertElementAt(" 7:00",2);
		ch1Time.insertElementAt(" 7:30",3);
		ch1Time.insertElementAt(" 8:00",4);
		ch1Time.insertElementAt(" 8:30",5);
		ch1Time.insertElementAt(" 9:00",6);
		ch1Time.insertElementAt(" 9:30",7);
		ch1Time.insertElementAt(" 10:00",8);
		ch1Time.insertElementAt(" 10:30",9);
		ch1Time.insertElementAt(" 11:00",10);
		ch1Time.insertElementAt(" 11:30",11);
		ch1Time.insertElementAt(" 12:00",12);
		ch1Time.insertElementAt(" 12:30",13);
		ch1Time.insertElementAt(" 13:00",14);
		ch1Time.insertElementAt(" 13:30",15);
		ch1Time.insertElementAt(" 14:00",16);
		ch1Time.insertElementAt(" 14:30",17);
		ch1Time.insertElementAt(" 15:00",18);
		ch1Time.insertElementAt(" 15:30",19);
		ch1Time.insertElementAt(" 16:00",20);
		ch1Time.insertElementAt(" 16:30",21);
		ch1Time.insertElementAt(" 17:00",22);
		ch1Time.insertElementAt(" 17:30",23);
		ch1Time.insertElementAt(" 18:00",24);
		ch1Time.insertElementAt(" 18:30",25);
		ch1Time.insertElementAt(" 19:00",26);
		ch1Time.insertElementAt(" 19:30",27);
		ch1Time.insertElementAt(" 20:00",28);
		ch1Time.insertElementAt(" 20:30",29);
		ch1Time.insertElementAt(" 21:00",30);
		ch1Time.insertElementAt(" 21:30",31);
		ch1Time.insertElementAt(" 22:00",32);
		ch1Time.insertElementAt(" 22:00",33);
		ch1Time.insertElementAt(" 23:00",34);
		ch1Time.insertElementAt(" 23:00",35);	

			try
			{
				
				BufferedReader reader;
				reader = new BufferedReader(new InputStreamReader(System.in));
	
				choice2 = Integer.parseInt(reader.readLine());


				if(choice2==1)
			{

	/*	for(int i = 0; i<ch1Time.size(); i++)
		{
			if(userTime.equals(ch1Time.elementAt(i))
			{
				
				return i;
			}		
		}*/

				schTime();
				}
			
				else if (choice2==2)
				{
				//
				}
								
				else if (choice2==3)
				{
				//
				}
				
				else if (choice2==4)
				{
				//			
				}
								
				else if (choice2==5)
				{
			//		
				}
				else if (choice2==6)
				{
				//			
				}
								
				else if (choice2==7)
				{
			//		
				}
				else if (choice2==0)
				{
					System.exit(0);			
				}
			}
			catch (Exception e)
			{
				System.out.println(e);
			}
		
		}while(choice2!=0);			
	}
	
	public void schTime()
	{
		System.out.println("ENTER TIME : ");

		try
		{
				
			BufferedReader reader;
			reader = new BufferedReader(new InputStreamReader(System.in));

			userTime = Integer.parseInt(reader.readLine());
			
			if(userTime.equals(ch1Time.elementAt(i)))
			{
				ch1Time.elementAt(i)= "";
				ch1Time.remove(i);
				ch1Time.insertElementAt("",i);
			}
			else
			{
				System.out.println(" Time is not available");
			}
		}
	
			catch (Exception e)
			{
				System.out.println(e);
			}

	}
		

	public void removeProgrammesCH1()
	{
		
	}
	
}

Dani AI

Generated

Brief diagnosis and an actionable fix.

The original compile errors come from two separate root causes. First, the "non‑static method cannot be referenced from a static context" is a static/instance issue — calling instance methods from main must be done on a Schedule object (as pointed out). Second, the runtime/compile problems inside schTime are scoping and type mistakes: i is used but never declared in that method, userTime is treated as a primitive int yet used with .equals(...), and ch1Time.elementAt(i) = ... is invalid Java (you can’t assign to a method result).

Practical, minimal changes that resolve the errors and make the logic clear:

  • Keep ch1Time / ch2Time as instance fields. Don’t make everything global; prefer method parameters and returns for transient values (echoes ’s advice).
  • Choose a consistent representation for times. If the vector stores strings like "6:00", read the input as a String and search the list for that string. If numeric comparisons are needed, convert both sides to the same numeric form (minutes since midnight or java.time.LocalTime).
  • Find the slot index explicitly (don’t rely on an undeclared i) and update the collection with the correct API (set / setElementAt), not assignment to elementAt(...).

Example (use List<String> for clarity):

// find the slot and clear it
String input = reader.readLine().trim();               // "6:00"
int idx = ch1Time.indexOf(input);
if (idx >= 0) {
    ch1Time.set(idx, "");        // or ch1Time.setElementAt("", idx) for Vector
} else {
    System.out.println("Time not available");
}

Design notes: prefer List<String> or, for modern Java, List<LocalTime> (java.time) and generics to avoid casting. Keep loop/temporary variables local; pass values between methods or return indices/booleans. These changes eliminate the static, scope, type and assignment errors seen in the thread.

Recommended Answers

All 4 Replies

The easiest way for this, and i think the most used, is to create an instance of your class and run the methods using that.

So it would be something like :

public class Schedule {

 Vector ch1Time  = new Vector();
 Vector ch2Time  = new Vector();
 
//Default constructor
public Schedule() {
}
  
	public static void main (String args[]) {
 
Schedule mySchedule = new Schedule();

mySchedule.mainMenu();
		
	}

etc.

Hope this helps.

Thanks.
i changed the code to this::

public class Schedule
{
		Vector ch1Time  = new Vector();
		Vector ch2Time  = new Vector();
                          int userTime;
		
	public static void main (String args[])
	{
		new Schedule().mainMenu();
		
	}
	

	public void mainMenu()
	{
		int choice1 = 0;

and it seemed to work, but now my only problem is the variable i problems that keep coming up..

Declaring it as a global variable does not seem to work..
Got ne ideas on this one...

always use the absolute minimum scope for variables that you can. NEVER EVER use globals unless there's really no way around it (and if there isn't your design is likely flawed).

If your method2 needs to work on data in a variable defined/declared (or whatever) in method1 you need to pass it as a argument.

JWenting is right though, have a look at your design. It looks like the Schedule object is the heart. So decide which data values are making up that object (like time, day, film tec.) and make these data-elemenst instance variables (like ch1Time and ch2Time are now). This will make them available to all instance methods.

Then if method2 needs to work on variable i declared/initialised in method1 you need to do something like :

method2(i);

in your method1 and declare method2 as method2(int i) or whatever the variable type is.

Hope this helps,
Guido

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.