Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Using the source code given, update the simple list to your top five chosen destinations. Include the destination title, a short description, and a small image for each. This will help ensure that you have fulfilled the acceptance criteria of the user story.
Add images for each destination and at least one additional customization, color scheme, or the like to make the control as elaborate as you want. Be sure to include clear and concise comments to explain your changes.

What I have tried:

here is my java project that I am trying to run as an application.
Java
  1  package src;
  2  import java.awt.*;
  3  import javax.swing.*;
  4  import javax.swing.border.*;
  5  
  6  
  7  public class TopFiveDestinationList {
  8      public static void main(String[] args) {
  9          SwingUtilities.invokeLater(new Runnable() {
 10              public void run() {
 11              	TopDestinationListFrame topDestinationListFrame = new TopDestinationListFrame();
 12                  topDestinationListFrame.setTitle("Top 5 Destination List");
 13                  topDestinationListFrame.setVisible(true);
 14              }
 15          });
 16      }
 17  }
 18  
 19  
 20  class TopDestinationListFrame extends JFrame {
 21      /**
 22  	 * 
 23  	 */
 24  	private static final long serialVersionUID = 1L;
 25  	private DefaultListModel<textandicon> listModel;
 26  
 27      public TopDestinationListFrame() {
 28          super("Top Five Destination List");
 29  
 30          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 31          setSize(900, 750);
 32  
 33          listModel = new DefaultListModel<textandicon>();
 34  
 35  
 36       // Custom color scheme for the list background and selection
 37          Color listBackground = new Color(245, 245, 245); // Light gray background
 38          Color selectionBackground = new Color(32, 136, 203); // Blue selection background
 39          Color selectionForeground = Color.WHITE; // White text for selection
 40  
 41          // Adding destinations with images and descriptions
 42          addDestinationNameAndPicture("1. Alborg, Denmark - A cultural gem with cobbled streets and modern art.", new ImageIcon(getClass().getResource("/resources/Aalborg.jpg")));
 43          addDestinationNameAndPicture("2. Cartagena, Colombia - A luxury escape with rich history and vibrant life.", new ImageIcon(getClass().getResource("/resources/Cartagena.jpg")));
 44          addDestinationNameAndPicture("3. Sonoma County, USA - Wine country with innovative businesses and beautiful landscapes.", new ImageIcon(getClass().getResource("/resources/Sonoma.jpg")));
 45          addDestinationNameAndPicture("4. Australian Outback - Remote adventures with expedition ships.", new ImageIcon(getClass().getResource("/resources/Outback.jpg")));
 46          addDestinationNameAndPicture("5. Himalayan Hideaway - Breathtaking altitudes and serene beauty.", new ImageIcon(getClass().getResource("/resources/Himalayas.jpg")));
 47  
 48          // Creating the JList and setting its properties
 49          JList<textandicon> list = new JList<>(listModel);
 50          list.setBackground(listBackground);
 51          list.setSelectionBackground(selectionBackground);
 52          list.setSelectionForeground(selectionForeground);
 53  
 54          // Custom cell renderer to handle TextAndIcon objects
 55          TextAndIconListCellRenderer renderer = new TextAndIconListCellRenderer(2);
 56          list.setCellRenderer(renderer);
 57  
 58          // Adding the list to a scroll pane for scrolling support
 59          JScrollPane scrollPane = new JScrollPane(list);
 60          getContentPane().add(scrollPane, BorderLayout.CENTER);
 61  
 62          // Label with your name
 63          JLabel nameLabel = new JLabel("Created by Jordan Schwartz");
 64          nameLabel.setHorizontalAlignment(JLabel.CENTER);
 65          getContentPane().add(nameLabel, BorderLayout.SOUTH);
 66      }
 67  
 68      private void addDestinationNameAndPicture(String text, ImageIcon icon) {
 69          TextAndIcon tai = new TextAndIcon(text, icon);
 70          listModel.addElement(tai);
 71      }
 72  }
 73  
 74  
 75  class TextAndIcon {
 76      private String text;
 77      private Icon icon;
 78  
 79      public TextAndIcon(String text, Icon icon) {
 80          this.text = text;
 81          this.icon = icon;
 82      }
 83  
 84      public String getText() {
 85          return text;
 86      }
 87  
 88      public Icon getIcon() {
 89          return icon;
 90      }
 91  
 92      public void setText(String text) {
 93          this.text = text;
 94      }
 95  
 96      public void setIcon(Icon icon) {
 97          this.icon = icon;
 98      }
 99  }
100  
101  
102  class TextAndIconListCellRenderer extends JLabel implements ListCellRenderer<object> {
103      /**
104  	 * 
105  	 */
106  	private static final long serialVersionUID = 1L;
107  
108  	private static final Border NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
109  
110      private Border insideBorder;
111  
112      public TextAndIconListCellRenderer() {
113          this(0, 0, 0, 0);
114      }
115  
116      public TextAndIconListCellRenderer(int padding) {
117          this(padding, padding, padding, padding);
118      }
119  
120      public TextAndIconListCellRenderer(int topPadding, int rightPadding, int bottomPadding, int leftPadding) {
121          insideBorder = BorderFactory.createEmptyBorder(topPadding, leftPadding, bottomPadding, rightPadding);
122          setOpaque(true);
123      }
124  
125      public Component getListCellRendererComponent(JList list, Object value,
126      int index, boolean isSelected, boolean hasFocus) {
127          // The object from the combo box model MUST be a TextAndIcon.
128          TextAndIcon tai = (TextAndIcon) value;
129  
130          // Sets text and icon on 'this' JLabel.
131          setText(tai.getText());
132          setIcon(tai.getIcon());
133  
134          if (isSelected) {
135              setBackground(list.getSelectionBackground());
136              setForeground(list.getSelectionForeground());
137          } else {
138              setBackground(list.getBackground());
139              setForeground(list.getForeground());
140          }
141  
142          Border outsideBorder;
143  
144          if (hasFocus) {
145              outsideBorder = UIManager.getBorder("List.focusCellHighlightBorder");
146          } else {
147              outsideBorder = NO_FOCUS_BORDER;
148          }
149  
150          setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
151          setComponentOrientation(list.getComponentOrientation());
152          setEnabled(list.isEnabled());
153          setFont(list.getFont());
154  
155          return this;
156      }
157  
158      // The following methods are overridden to be empty for performance
159      // reasons. If you want to understand better why, please read:
160      //
161      // http://java.sun.com/javase/6/docs/api/javax/swing/DefaultListCellRenderer.html#override
162  
163      public void validate() {}
164      public void invalidate() {}
165      public void repaint() {}
166      public void revalidate() {}
167      public void repaint(long tm, int x, int y, int width, int height) {}
168      public void repaint(Rectangle r) {}
169  }

But when I attempt to run this as a java application this is the error messages I get.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at src.TopDestinationListFrame.<init>(TopFiveDestinationList.java:42)
	at src.TopFiveDestinationList$1.run(TopFiveDestinationList.java:11)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

Please help me. Thank you.
Posted
Updated 21-Mar-24 6:09am
v2
Comments
Richard Deeming 21-Mar-24 12:20pm    
The error and line number suggests that getClass().getResource("/resources/Aalborg.jpg") is returning null.

You need to debug your code to work out why. Nobody else can do that for you.

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and your IDE will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.
 
Share this answer
 
Comments
Jordan Schwartz 2024 22-Mar-24 2:58am    
Thank you very much for taking the time to help me. I'm sure it is quite obvious I am a novice at java and running java applications using computer code. However, your analogy helped me immensely with understanding what and how to correct my code and also, the tip about using the debugger tool was also extremely helpful. So thank you again!
OriginalGriff 22-Mar-24 3:21am    
You're welcome - we all had to start from the beginning once! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900