Results 1 to 6 of 6

Thread: Link list help

  1. #1
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Link list help

    I am having issues running through a link list for one of my assignments. I need to create a program that displays 4 images (each image is a node in the link list). After displaying the images the user is allowed to cut the middle or last picture which deletes the node and moves its data into a "clipboard". Essentially the same concept for cutting and pasting pictures on windows. Whenever I run the program it works up until I try to cut the pictures at which point I get a java.lang.NullPointerException and I am not quite sure how to fix the methods to make it work (lines 76-93). I was also having issues with how to paste the image. The paste function always pastes the picture at the end of the list so I figured I could use the same method addLastPic which adds a picture/node to the end of the list but I was not sure how to set up line 141 in order to make it paste whats in the clipboard. Here's the code:

    Java Code:
    import java.util.Scanner;
    
    class LineCollage extends Picture
    {
      private class Node
      {
        Picture data;
        Node pNext; 
      }      
      private int nPictures;
      private Node pNodeIn;
      private Node pFirst;
      private Node pLast;
      private Node tmp;
      private Node clipboard;
      
    //adds picture at the end of link list 
      public void addLastPic(Picture aPictureReference) {
        pNodeIn = new Node();
        pNodeIn.data = aPictureReference;
        if(pFirst==null)
        {
          pFirst = pNodeIn;
          pLast = pNodeIn;
        }
        else {
          pLast.pNext=pNodeIn;
          pLast=pNodeIn;
        }
        nPictures++;
      }
      
      public Picture makeCollage()
      {
        int collageHeight;
        int collageWidth;
        int collageCenter;
        int pictX = 0;
        int maxHeight = 0;
        int maxWidth = 0;
        
        //sets width and height  
        tmp = pFirst;
        for(int i = 0; i < nPictures; i++)
        {
          if(tmp.data.getHeight() > maxHeight)
          {
            maxHeight = tmp.data.getHeight();
          }
          tmp=tmp.pNext;
        }
        collageHeight = maxHeight;
        tmp=pFirst;
        
        //finds maxHeight    
        for(int i = 0; i < nPictures; i++)
        {
          maxWidth = maxWidth + tmp.data.getWidth(); 
          tmp=tmp.pNext;
        }
        
        collageWidth = maxWidth;
        Picture retval = new Picture(collageWidth,collageHeight);
        collageCenter = retval.getHeight()/2;
        tmp=pFirst;
        for(int i = 0; i < nPictures; i++) //Composes the pictures in to the collage
        {
          tmp.data.compose(retval,pictX,collageCenter-tmp.data.getHeight()/2);
          pictX += tmp.data.getWidth();
          tmp=tmp.pNext;
        }
        return retval;
      }
      //---------------------------Project 4 Methods---------------------------------//
      
    //Cuts middle picture
      public void cutMiddle(){
        for ( int i = nPictures/2; i<1; i-- )
          tmp=tmp.pNext;
        tmp.pNext = tmp.pNext.pNext;
        clipboard=tmp.pNext;
        nPictures--;
        return; 
      }
      
      //Cuts last picture
      public void cutLast(){
        for (int i=nPictures-1; i<i; i--)
          tmp.pNext = null;
        clipboard=tmp.pNext;
        nPictures--;
        return;
      }
      
      //--------------------------------------------------------------------------//
      
    //Main Method
      public static void main(String[]a)
      {
        LineCollage myCollage;
        Scanner uiInput = new Scanner(System.in);
        myCollage = new LineCollage();
        FileChooser.pickMediaPath();
        boolean input = true;
        while( input ) {
          System.out.println("Type Y if you want another picture or N if you don't.");
          System.out.println("If you want to cut the middle picture then type CM.");
          System.out.println("If you want to cut the last picture type CE.");
          System.out.println("If you want to paste a picture to the end of the collage type PE.");
          String answer = uiInput.next();
          if(answer.equals("Y"))
          {
            Picture pin = new Picture(FileChooser.pickAFile());
            myCollage.addLastPic(pin);
            myCollage.makeCollage().show();
          } 
          //XX
          else if(answer.equals("N"))
          {
            input = false;
            System.out.println("You chose to stop adding pictures.");
            System.out.println("Type XX to close the program");
            String answer1 = uiInput.next();
            if(answer1.equals("XX"))
            {
              System.exit(0);
            }
          }
          //CM
          if (answer.equals("CM")) {
            myCollage.cutMiddle();
            myCollage.makeCollage().show();
          }
          //CE
          if (answer.equals("CE")){
            myCollage.cutLast();
            myCollage.makeCollage().show();
          }
          //PE
          if (answer.equals("PE")){
            //myCollage.addLastPic(data.clipboard);
            myCollage.makeCollage().show();
          }
        }
      }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Link list help

    I get a java.lang.NullPointerException
    Please post the full text of the error message.
    Look at the line in the code where the error occurs and find the variable with the null value. Then backtrack to see why the variable does not have a non-null value.

  3. #3
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Link list help

    If I do the CM command it points to line 80 and then 131 and I'm not sure why they have a null value. It does give me a warning that the field LineCollage.clipboard is never read locally. Does that mean that the link list isn't copying the data into the clipboard properly? If it does im not quite sure what to change.

    java.lang.NullPointerException
    at LineCollage.cutLast(LineCollage2.java:90)
    at LineCollage.main(LineCollage2.java:136)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Link list help

    java.lang.NullPointerException
    at LineCollage.cutLast(LineCollage2.java:90)
    This error occurred on line 90. Find the variable with the null value and then backtrack.

  5. #5
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Link list help

    Ok i figured that out i changed the cutLast method to this which now works and cuts the last picture.

    Java Code:
     public void cutLast(){
        for (int i=nPictures-1; i<i; i--)
          pLast.pNext = null;
        clipboard=pLast;
        nPictures--;
        return;
      }
    I am confused with lines 79 and 80. I know that java has garbage collection and that I need to skip over the middle node but im not sure how. I think there might be one variable that I am miss using in the method. Also what is the proper syntax for line 141 to make the addLastPic method paste what is in the clipboard? I still get the warning that clipboard is never read. On lines 113-115 i have it set so that the variable pin causes the filechooser to open up however whenever i try modifying that code with clipboard it tells me i cant make a static reference to the non static field clipboard

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Link list help

    cant make a static reference to the non static field clipboard
    non static fields only exist when there is an instance of a class.
    static methods don't require an instance of a class.

    If you want to reference a non static field in a class from a static method, you need to create an instance of the class and use the reference to that instance to reference the field.

Similar Threads

  1. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 06:37 AM
  2. Replies: 3
    Last Post: 11-21-2009, 06:48 PM
  3. How to link a Array elemant to a Linked list Node
    By ravinda in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 10:16 AM
  4. Link List
    By one198 in forum New To Java
    Replies: 0
    Last Post: 10-14-2007, 02:33 PM

Posting Permissions

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