The Artima Developer Community
Sponsored Link

Java Answers Forum
help with an error

1 reply on 1 page. Most recent reply: May 2, 2003 11:10 AM by Erik Price

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

help with an error Posted: May 2, 2003 11:03 AM
Reply to this message Reply
Advertisement
I am getting this error:
List.java:259: non-static variable this cannot be referenced from a static context
Link newl=new Link(sString);

/******************************************************
 Name: List
 Project number: 4
 Submission date: today
 Contents: List(), add(),remove(), isEmpty(), display()
 	pop()
 ******************************************************/
 
public class List 
{
	private Link head;
	
	public List()
	{
		head=null;
	}
	
	/******************************************************
 	Name: Link
 	Project number: 4
 	Submission date: today
 	Contents: Link(), setNext(), getNext(), setPrev(),
 		getPrev()
 	******************************************************/
	public class Link
	{
	
		Object value;
		private Link next, prev;
	
		public Link(Object v)
		{
			next=null;
			prev=null;
			this.value=v;
		}
	
		public void setNext(Link n)
		{
			this.next=n;
		}
	
		public void setPrev(Link p)
		{
			this.prev=p;
		}	
	
		public Link getNext()
		{
			return next;
		}
	
		public Link getPrev()
		{
			return prev;
		}
	}
 
	/**************************************************
	 Name: add()
	 Parameters: Link
	 Declared variables: Link temp
	 Return value: void
	 Algorithm:
	 	check if there is anything in the list.
	 	if there isn't then make head point to the specified
	 		link.
	 	else there is something, at least one item in the list.
	 	check if the item that head is pointing to's next
	 	is equal to null if it isn't then make the item that head 
	 	is pointing's next equal to the specified link. 
	 	else check if the item that head's pointing to's next is 
	 	equal to null, if not then make the specified equal to that 
	 	next.
	 
	 **************************************************/
	public void add(Link l)
	{
		
		Link temp=head;
 
		if(head==null)
		{
			head=l;
		}
		else
		{  
			while(temp.getNext()!=null)
			{
				temp = temp.getNext();
			}
				
			temp.setNext(l);
			l.setPrev(temp);
		}
 
	}
	
	/**************************************************
	 Name: remove()
	 Parameters: int
	 Declared variables: Link temp
	 Return value: void
	 Algorithm:
	 	cycle through the list checking every value of every 
	 		link in the list, when the value matches the 
	 		specified remove the links to it.
	 		and set them.
	 **************************************************/
	public void remove(Object enteredValue)
	{
		Link temp=head;
		if(isEmpty())
		{
			return;
		}
		else if(temp.value.equals(enteredValue))
		{
			head=temp.getNext();
		}
		else
		{
			while(!temp.value.equals(enteredValue))
			{
				temp=temp.getNext();
			}
			if(temp.getNext()!=null)
			{
				temp.getNext().setPrev(temp.getPrev());
				temp.getPrev().setNext(temp.getNext());
			}
			else
			{
				temp.getPrev().setNext(null);
			}
			
		}
	}
	
	/**************************************************
	 Name: isEmpty()
	 Parameters: none
	 Declared variables: none
	 Return value: boolean
	 Algorithm:none
	 **************************************************/
	public boolean isEmpty()
	{
		return head==null;
	}
	
	/**************************************************
	 Name: display()
	 Parameters: int
	 Declared variables: Link temp
	 Return value: void
	 Algorithm:
	 	cycle through the list printing every value in it.
	 **************************************************/
	public void display()
	{
		Link temp=head;
		System.out.println("--The list--");
		if(isEmpty())
		{
			System.out.println("List is empty");
		}
		else
		{
			while(temp.getNext()!=null)
			{
				System.out.print(temp.value +" ");
				temp = temp.getNext();
			
			}
			System.out.print(temp.value +" ");
			System.out.print("null");
			System.out.println();
		}
    }
    
    /**************************************************
	 Name: pop()
	 Parameters: none
	 Declared variables: Link temp
	 Return value: void
	 Algorithm:
	 	Cycle through to the end of the list
	 		delete the pointers to the last item
	 		then return the last item.
	 **************************************************/
    public void pop()
    {
    	Link temp=head;
    	while(temp.getNext()!=null)
		{
			temp = temp.getNext();
		}
		if(temp.getPrev()==null)
		{
			head=null;
		}
		else
		{
			
			System.out.println("Popped: "+temp.value);
			temp.getPrev().setNext(null);
		}
		
	}
	
	/**************************************************
	 Name: clear()
	 Parameters: none
	 Declared variables: none
	 Return value: void
	 Algorithm:none
	 **************************************************/
	public void clear()
	{
		head=null;
	}
	
	/**************************************************
	 Name: contains()
	 Parameters: int
	 Declared variables: Link temp
	 Return value: boolean
	 Algorithm:
	 	cycle through the list checking every value of every 
	 		link in the list, when the value matches the 
	 		specified remove the links to it.
	 		and set them.
	 **************************************************/
	public boolean contains(Object v)
	{
		Link temp=head;
		if(isEmpty())
		{
			System.out.println("List is empty!");
		}
		else
		{
			while(temp!=null)
			{
				if(temp.value.equals(v))
				{
					return true;
				}
				temp=temp.getNext();
			}
		}
		return false;
	}
	
	public static void main(String[] args){
		List list=new List();
		String sString=new String("hello");
		Link newl=new Link(sString);
		list.add(newl);
	}
 
}


someone help me out here, I am tired of looking at this code.


Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: help with an error Posted: May 2, 2003 11:10 AM
Reply to this message Reply
The problem is that you have a non-static inner class named "Link" in your class "List". In order to access a non-static inner class, even simply to instantiate it, you need a reference to an instance of the enclosing class. So one way you could do it is:
public static void main(String[] args) {
  List ist = new List();
  Link ink = ist.new Link();
}

If you don't specifically require that your non-static inner class be non-static, an easier way to do this would be to make it into a nested static class:
public class List {
  public static class Link {
     ...
  }
  ...
}


The simple lesson here is that, like non-static fields and non-static methods, a non-static inner class cannot be referenced or used from a static context like public static void main.

Flat View: This topic has 1 reply on 1 page
Topic: Writing to a file. Previous Topic   Next Topic Topic: Problem in Java Chat Application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use