Results 1 to 12 of 12
Thread: null pointer exception
- 02-03-2012, 07:27 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
null pointer exception
Hey, when i run this i get a nullPointerException.
help?
heres the code...
tksJava Code:package main; import java.awt.EventQueue; /* * main Gui for calender * * */ @SuppressWarnings("serial") public class MainUi extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainUi frame = new MainUi(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) {} } /** * Create the frame. * * Make the components */ public MainUi() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 710, 263); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); gbl_contentPane.columnWidths = new int[]{98, 100, 100, 100, 100, 100, 100, 0}; gbl_contentPane.rowHeights = new int[]{0, 25, 0, 0, 0, 0, 0, 0, 0}; gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; contentPane.setLayout(gbl_contentPane); JButton backbutton = new JButton("<<"); GridBagConstraints gbc_backbutton = new GridBagConstraints(); gbc_backbutton.fill = GridBagConstraints.BOTH; gbc_backbutton.insets = new Insets(0, 0, 5, 5); gbc_backbutton.gridx = 0; gbc_backbutton.gridy = 0; contentPane.add(backbutton, gbc_backbutton); JLabel month = new JLabel("January"); GridBagConstraints gbc_month = new GridBagConstraints(); gbc_month.gridwidth = 5; gbc_month.insets = new Insets(0, 0, 5, 5); gbc_month.gridx = 1; gbc_month.gridy = 0; contentPane.add(month, gbc_month); JButton forewardButton = new JButton(">>"); GridBagConstraints gbc_forewardButton = new GridBagConstraints(); gbc_forewardButton.insets = new Insets(0, 0, 5, 0); gbc_forewardButton.fill = GridBagConstraints.BOTH; gbc_forewardButton.gridx = 6; gbc_forewardButton.gridy = 0; contentPane.add(forewardButton, gbc_forewardButton); JLabel lblSunday = new JLabel("Sunday"); GridBagConstraints gbc_lblSunday = new GridBagConstraints(); gbc_lblSunday.insets = new Insets(0, 0, 5, 5); gbc_lblSunday.gridx = 0; gbc_lblSunday.gridy = 1; contentPane.add(lblSunday, gbc_lblSunday); JLabel monday = new JLabel("Monday"); GridBagConstraints gbc_monday = new GridBagConstraints(); gbc_monday.fill = GridBagConstraints.VERTICAL; gbc_monday.insets = new Insets(0, 0, 5, 5); gbc_monday.gridx = 1; gbc_monday.gridy = 1; contentPane.add(monday, gbc_monday); JLabel teusday = new JLabel("Teusday"); GridBagConstraints gbc_teusday = new GridBagConstraints(); gbc_teusday.fill = GridBagConstraints.VERTICAL; gbc_teusday.insets = new Insets(0, 0, 5, 5); gbc_teusday.gridx = 2; gbc_teusday.gridy = 1; contentPane.add(teusday, gbc_teusday); JLabel wendsday = new JLabel("Wendsday"); GridBagConstraints gbc_wendsday = new GridBagConstraints(); gbc_wendsday.fill = GridBagConstraints.VERTICAL; gbc_wendsday.insets = new Insets(0, 0, 5, 5); gbc_wendsday.gridx = 3; gbc_wendsday.gridy = 1; contentPane.add(wendsday, gbc_wendsday); JLabel thursday = new JLabel("Thursday"); GridBagConstraints gbc_thursday = new GridBagConstraints(); gbc_thursday.insets = new Insets(0, 0, 5, 5); gbc_thursday.gridx = 4; gbc_thursday.gridy = 1; contentPane.add(thursday, gbc_thursday); JLabel friday = new JLabel("Friday"); GridBagConstraints gbc_friday = new GridBagConstraints(); gbc_friday.insets = new Insets(0, 0, 5, 5); gbc_friday.gridx = 5; gbc_friday.gridy = 1; contentPane.add(friday, gbc_friday); JLabel saturday = new JLabel("Saturday"); GridBagConstraints gbc_saturday = new GridBagConstraints(); gbc_saturday.insets = new Insets(0, 0, 5, 0); gbc_saturday.gridx = 6; gbc_saturday.gridy = 1; contentPane.add(saturday, gbc_saturday); JButton day1 = new JButton("1"); GridBagConstraints gbc_day1 = new GridBagConstraints(); gbc_day1.fill = GridBagConstraints.HORIZONTAL; gbc_day1.insets = new Insets(0, 0, 5, 5); gbc_day1.gridx = 0; gbc_day1.gridy = 2; contentPane.add(day1, gbc_day1); JButton day2 = new JButton("2"); GridBagConstraints gbc_day2 = new GridBagConstraints(); gbc_day2.fill = GridBagConstraints.HORIZONTAL; gbc_day2.insets = new Insets(0, 0, 5, 5); gbc_day2.gridx = 1; gbc_day2.gridy = 2; contentPane.add(day2, gbc_day2); JButton day3 = new JButton("3"); GridBagConstraints gbc_day3 = new GridBagConstraints(); gbc_day3.fill = GridBagConstraints.HORIZONTAL; gbc_day3.insets = new Insets(0, 0, 5, 5); gbc_day3.gridx = 2; gbc_day3.gridy = 2; contentPane.add(day3, gbc_day3); JButton day4 = new JButton("4"); GridBagConstraints gbc_day4 = new GridBagConstraints(); gbc_day4.fill = GridBagConstraints.HORIZONTAL; gbc_day4.insets = new Insets(0, 0, 5, 5); gbc_day4.gridx = 3; gbc_day4.gridy = 2; contentPane.add(day4, gbc_day4); JButton day5 = new JButton("5"); GridBagConstraints gbc_day5 = new GridBagConstraints(); gbc_day5.fill = GridBagConstraints.HORIZONTAL; gbc_day5.insets = new Insets(0, 0, 5, 5); gbc_day5.gridx = 4; gbc_day5.gridy = 2; contentPane.add(day5, gbc_day5); JButton day6 = new JButton("6"); GridBagConstraints gbc_day6 = new GridBagConstraints(); gbc_day6.fill = GridBagConstraints.HORIZONTAL; gbc_day6.insets = new Insets(0, 0, 5, 5); gbc_day6.gridx = 5; gbc_day6.gridy = 2; contentPane.add(day6, gbc_day6); JButton day7 = new JButton("7"); GridBagConstraints gbc_day7 = new GridBagConstraints(); gbc_day7.fill = GridBagConstraints.HORIZONTAL; gbc_day7.insets = new Insets(0, 0, 5, 0); gbc_day7.gridx = 6; gbc_day7.gridy = 2; contentPane.add(day7, gbc_day7); JButton day8 = new JButton("8"); GridBagConstraints gbc_day8 = new GridBagConstraints(); gbc_day8.fill = GridBagConstraints.HORIZONTAL; gbc_day8.insets = new Insets(0, 0, 5, 5); gbc_day8.gridx = 0; gbc_day8.gridy = 3; contentPane.add(day8, gbc_day8); JButton day9 = new JButton("9"); GridBagConstraints gbc_day9 = new GridBagConstraints(); gbc_day9.fill = GridBagConstraints.HORIZONTAL; gbc_day9.insets = new Insets(0, 0, 5, 5); gbc_day9.gridx = 1; gbc_day9.gridy = 3; contentPane.add(day9, gbc_day9); JButton day10 = new JButton("10"); GridBagConstraints gbc_day10 = new GridBagConstraints(); gbc_day10.fill = GridBagConstraints.HORIZONTAL; gbc_day10.insets = new Insets(0, 0, 5, 5); gbc_day10.gridx = 2; gbc_day10.gridy = 3; contentPane.add(day10, gbc_day10); JButton day11 = new JButton("11"); GridBagConstraints gbc_day11 = new GridBagConstraints(); gbc_day11.fill = GridBagConstraints.HORIZONTAL; gbc_day11.insets = new Insets(0, 0, 5, 5); gbc_day11.gridx = 3; gbc_day11.gridy = 3; contentPane.add(day11, gbc_day11); JButton day12 = new JButton("12"); GridBagConstraints gbc_day12 = new GridBagConstraints(); gbc_day12.fill = GridBagConstraints.HORIZONTAL; gbc_day12.insets = new Insets(0, 0, 5, 5); gbc_day12.gridx = 4; gbc_day12.gridy = 3; contentPane.add(day12, gbc_day12); JButton day13 = new JButton("13"); GridBagConstraints gbc_day13 = new GridBagConstraints(); gbc_day13.fill = GridBagConstraints.HORIZONTAL; gbc_day13.insets = new Insets(0, 0, 5, 5); gbc_day13.gridx = 5; gbc_day13.gridy = 3; contentPane.add(day13, gbc_day13); JButton day14 = new JButton("14"); GridBagConstraints gbc_day14 = new GridBagConstraints(); gbc_day14.fill = GridBagConstraints.HORIZONTAL; gbc_day14.insets = new Insets(0, 0, 5, 0); gbc_day14.gridx = 6; gbc_day14.gridy = 3; contentPane.add(day14, gbc_day14); JButton day15 = new JButton("15"); GridBagConstraints gbc_day15 = new GridBagConstraints(); gbc_day15.fill = GridBagConstraints.HORIZONTAL; gbc_day15.insets = new Insets(0, 0, 5, 5); gbc_day15.gridx = 0; gbc_day15.gridy = 4; contentPane.add(day15, gbc_day15); JButton day16 = new JButton("16"); GridBagConstraints gbc_day16 = new GridBagConstraints(); gbc_day16.fill = GridBagConstraints.HORIZONTAL; gbc_day16.insets = new Insets(0, 0, 5, 5); gbc_day16.gridx = 1; gbc_day16.gridy = 4; contentPane.add(day16, gbc_day16); JButton day17 = new JButton("17"); GridBagConstraints gbc_day17 = new GridBagConstraints(); gbc_day17.fill = GridBagConstraints.HORIZONTAL; gbc_day17.insets = new Insets(0, 0, 5, 5); gbc_day17.gridx = 2; gbc_day17.gridy = 4; contentPane.add(day17, gbc_day17); JButton day18 = new JButton("18"); GridBagConstraints gbc_day18 = new GridBagConstraints(); gbc_day18.fill = GridBagConstraints.HORIZONTAL; gbc_day18.insets = new Insets(0, 0, 5, 5); gbc_day18.gridx = 3; gbc_day18.gridy = 4; contentPane.add(day18, gbc_day18); JButton day19 = new JButton("19"); GridBagConstraints gbc_day19 = new GridBagConstraints(); gbc_day19.fill = GridBagConstraints.HORIZONTAL; gbc_day19.insets = new Insets(0, 0, 5, 5); gbc_day19.gridx = 4; gbc_day19.gridy = 4; contentPane.add(day19, gbc_day19); JButton day20 = new JButton("20"); GridBagConstraints gbc_day20 = new GridBagConstraints(); gbc_day20.fill = GridBagConstraints.HORIZONTAL; gbc_day20.insets = new Insets(0, 0, 5, 5); gbc_day20.gridx = 5; gbc_day20.gridy = 4; contentPane.add(day20, gbc_day20); JButton day21 = new JButton("21"); GridBagConstraints gbc_day21 = new GridBagConstraints(); gbc_day21.fill = GridBagConstraints.HORIZONTAL; gbc_day21.insets = new Insets(0, 0, 5, 0); gbc_day21.gridx = 6; gbc_day21.gridy = 4; contentPane.add(day21, gbc_day21); JButton day22 = new JButton("22"); GridBagConstraints gbc_day22 = new GridBagConstraints(); gbc_day22.fill = GridBagConstraints.HORIZONTAL; gbc_day22.insets = new Insets(0, 0, 5, 5); gbc_day22.gridx = 0; gbc_day22.gridy = 5; contentPane.add(day22, gbc_day22); JButton day23 = new JButton("23"); GridBagConstraints gbc_day23 = new GridBagConstraints(); gbc_day23.fill = GridBagConstraints.HORIZONTAL; gbc_day23.insets = new Insets(0, 0, 5, 5); gbc_day23.gridx = 1; gbc_day23.gridy = 5; contentPane.add(day23, gbc_day23); JButton day24 = new JButton("24"); GridBagConstraints gbc_day24 = new GridBagConstraints(); gbc_day24.fill = GridBagConstraints.HORIZONTAL; gbc_day24.insets = new Insets(0, 0, 5, 5); gbc_day24.gridx = 2; gbc_day24.gridy = 5; contentPane.add(day24, gbc_day24); JButton day25 = new JButton("25"); GridBagConstraints gbc_day25 = new GridBagConstraints(); gbc_day25.fill = GridBagConstraints.HORIZONTAL; gbc_day25.insets = new Insets(0, 0, 5, 5); gbc_day25.gridx = 3; gbc_day25.gridy = 5; contentPane.add(day25, gbc_day25); JButton day26 = new JButton("26"); GridBagConstraints gbc_day26 = new GridBagConstraints(); gbc_day26.fill = GridBagConstraints.HORIZONTAL; gbc_day26.insets = new Insets(0, 0, 5, 5); gbc_day26.gridx = 4; gbc_day26.gridy = 5; contentPane.add(day26, gbc_day26); JButton day27 = new JButton("27"); GridBagConstraints gbc_day27 = new GridBagConstraints(); gbc_day27.fill = GridBagConstraints.HORIZONTAL; gbc_day27.insets = new Insets(0, 0, 5, 5); gbc_day27.gridx = 5; gbc_day27.gridy = 5; contentPane.add(day27, gbc_day27); JButton day28 = new JButton("28"); GridBagConstraints gbc_day28 = new GridBagConstraints(); gbc_day28.fill = GridBagConstraints.HORIZONTAL; gbc_day28.insets = new Insets(0, 0, 5, 0); gbc_day28.gridx = 6; gbc_day28.gridy = 5; contentPane.add(day28, gbc_day28); JButton day29 = new JButton("29"); GridBagConstraints gbc_day29 = new GridBagConstraints(); gbc_day29.fill = GridBagConstraints.HORIZONTAL; gbc_day29.insets = new Insets(0, 0, 5, 5); gbc_day29.gridx = 0; gbc_day29.gridy = 6; contentPane.add(day29, gbc_day29); JButton day30 = new JButton("30"); GridBagConstraints gbc_day30 = new GridBagConstraints(); gbc_day30.fill = GridBagConstraints.HORIZONTAL; gbc_day30.insets = new Insets(0, 0, 5, 5); gbc_day30.gridx = 1; gbc_day30.gridy = 6; contentPane.add(day30, gbc_day30); JButton day31 = new JButton("31"); GridBagConstraints gbc_day31 = new GridBagConstraints(); gbc_day31.fill = GridBagConstraints.HORIZONTAL; gbc_day31.insets = new Insets(0, 0, 5, 5); gbc_day31.gridx = 2; gbc_day31.gridy = 6; contentPane.add(day31, gbc_day31); JButton day32 = new JButton("New button"); GridBagConstraints gbc_day32 = new GridBagConstraints(); gbc_day32.fill = GridBagConstraints.HORIZONTAL; gbc_day32.insets = new Insets(0, 0, 5, 5); gbc_day32.gridx = 3; gbc_day32.gridy = 6; contentPane.add(day32, gbc_day32); day32.setVisible(false); JButton day33 = new JButton("New button"); GridBagConstraints gbc_day33 = new GridBagConstraints(); gbc_day33.fill = GridBagConstraints.HORIZONTAL; gbc_day33.insets = new Insets(0, 0, 5, 5); gbc_day33.gridx = 4; gbc_day33.gridy = 6; contentPane.add(day33, gbc_day33); day33.setVisible(false); JButton day34 = new JButton("New button"); GridBagConstraints gbc_day34 = new GridBagConstraints(); gbc_day34.fill = GridBagConstraints.HORIZONTAL; gbc_day34.insets = new Insets(0, 0, 5, 5); gbc_day34.gridx = 5; gbc_day34.gridy = 6; contentPane.add(day34, gbc_day34); day34.setVisible(false); JButton day35 = new JButton("New button"); GridBagConstraints gbc_day35 = new GridBagConstraints(); gbc_day35.fill = GridBagConstraints.HORIZONTAL; gbc_day35.insets = new Insets(0, 0, 5, 0); gbc_day35.gridx = 6; gbc_day35.gridy = 6; contentPane.add(day35, gbc_day35); day35.setVisible(false); JButton day36 = new JButton("New button"); GridBagConstraints gbc_day36 = new GridBagConstraints(); gbc_day36.fill = GridBagConstraints.HORIZONTAL; gbc_day36.insets = new Insets(0, 0, 0, 5); gbc_day36.gridx = 0; gbc_day36.gridy = 7; contentPane.add(day36, gbc_day36); day36.setVisible(false); JButton day37 = new JButton("New button"); GridBagConstraints gbc_day37 = new GridBagConstraints(); gbc_day37.fill = GridBagConstraints.HORIZONTAL; gbc_day37.insets = new Insets(0, 0, 0, 5); gbc_day37.gridx = 1; gbc_day37.gridy = 7; contentPane.add(day37, gbc_day37); day37.setVisible(false); } }
- 02-03-2012, 07:34 PM #2
Re: null pointer exception
You forgot to post the full text of the error message which has a lot of info that will help solve the problem.
The message gives the line number where there is a variable with a null value. You need to find that variable and then backtrack in the code to see why it does not have a valid value.
- 02-03-2012, 07:34 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
Re: null pointer exception
Where abouts do you get the null pointer exception? Please be more specific, and if possible can you get rid of the code that doesn't throw the exception. Not all of it obviously but just so it makes your code a little bit less like a random wall of text :)
- 02-03-2012, 07:55 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 14
- Rep Power
- 0
Re: null pointer exception
All i did was that i placed all your grid bag code in a try catch block. and it worked. now i dont know what this software actually does so dont know if this helped. To get a proper answer try to explain what your code is expected to do.
- 02-03-2012, 07:59 PM #5
Re: null pointer exception
What code is in the catch block? What exception did it catch? Did it call printStackTrace() to show the stack trace and the exception. If not, you are only fooling your self. The error could still be there, you're just ignoring it.
- 02-03-2012, 08:07 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 14
- Rep Power
- 0
- 02-03-2012, 08:10 PM #7
Re: null pointer exception
@farhanz2009 Did your code throw an exception and did you get a stack trace? You could post that and help the OP find the problem
- 02-04-2012, 03:26 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
Re: null pointer exception
The code is just supposed to make a user interface. I have not made it do anything else
- 02-04-2012, 03:28 PM #9
Re: null pointer exception
If you need help with the exception your program gets, you need to post the full text of the error message here.
-
Re: null pointer exception
!!!!!Java Code:try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) {} // !!!!! Yikes !!!!!
You almost never ignore exceptions, unless you have a very strong reason for doing so (and you certainly don't).
Please change those lines to something like:
Java Code:try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); }
-
Re: null pointer exception
Also much of your code is quite redundant and I would try to consolidate things if I were you. For instance, consider...
- creating a createGbc method with a signature of:
Java Code:private GridBagConstraints createGbc(int gridx, int gridy, Insets insets) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = gridx; gbc.gridy = gridy; gbc.gridwidth = 1; gbc.gridheight = // ... etc ... gbc.fill = GridBagConstraints.BOTH; gbc.anchor = // ... etc ... gbc.insets = insets; gbc.weightx = // ... etc ... gbc.weighty = // ... etc ... return gbc; } - using constants for your insets:
Java Code:private static final int I_GAP = 5; private static final Insets STD_INSETS = new Insets(0, 0, I_GAP, I_GAP); private static final Insets RIGHT_INSETS = new Insets(0, 0, I_GAP, 0);
- using an array of String for your days of the week JLabels, and loop through that array to create and place the labels:
Java Code:for (int i = 0; i < DAYS_OF_WEEK.length; i++) { Insets insets = (i < DAYS_OF_WEEK.length - 1) ? STD_INSETS : RIGHT_INSETS; //... etc... } - giving your class a constructor that accepts the month String, the number of days in the month, and the start position for the first day of the month (an int from 0 to 6), so that it would have a signature like so:
Java Code:public MainUi(String month, int noOfDaysInMonth, int startPosition) { // TODO: fill in the rest! } - having your class create a JPanel so that the main GUI can swap JPanels via a CardLayout.
- using a for loop to create your days of the month button. You'll need to use the number of days in the month parameter for this and the start position parameter and of course your createGbc(...) method. The first part of this method would create empty JLabels for padding if the startPosition is > 0.
With these changes, I've been able to create a program that does the same thing as yours but that has one fourth the amount code. And it's a lot easier to modify and debug.Last edited by Fubarable; 02-04-2012 at 05:11 PM.
- creating a createGbc method with a signature of:
-
Similar Threads
-
Null pointer exception
By Domo230 in forum New To JavaReplies: 4Last Post: 02-28-2011, 10:21 AM -
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Help with Null Pointer Exception
By Beginner in forum New To JavaReplies: 2Last Post: 04-17-2010, 04:41 PM -
Null pointer exception
By talha06 in forum JDBCReplies: 5Last Post: 07-14-2009, 01:12 AM -
Null Pointer Exception
By demiser55 in forum New To JavaReplies: 1Last Post: 09-22-2008, 06:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks