Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default nullpointer but text is present

    Java Code:
    if (sc.getClass().getName().equals(
                                            "javax.swing.JComboBox"))  {
                        JComboBox source = (JComboBox) sc;
                       
                        JRadioButtonMenuItem rb = (JRadioButtonMenuItem)
                                                    source.getSelectedItem(); // I added radio buttons to the combo box
                        System.out.println("rb is <" + rb);
                        currentFont = rb.getText();
                    }
    The print line shows:

    rb is <javax.swing.JRadioButtonMenuItem[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,bor der=com.apple.laf.AquaMenuBorder@5e95215b,flags=26 4,maximumSize=,minimumSize=,preferredSize=,default Icon=,disabledIcon=,disabledSelectedIcon=,margin=j avax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=false,pressedIcon=,ro lloverEnabled=false,rolloverIcon=,rolloverSelected Icon=,selectedIcon=,text=Adobe Caslon Pro]

    But when I like to get the text at the final part of the string (see line 8) I get
    a nullpointer, why?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at willemwriter.MenuFontMenu$MenuActionListener.actio nPerformed(MenuFontMenu.java:359)
    at javax.swing.JComboBox.fireActionEvent(JComboBox.ja va:1240)
    at javax.swing.JComboBox.contentsChanged(JComboBox.ja va:1311)
    at javax.swing.JComboBox.intervalRemoved(JComboBox.ja va:1331)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: nullpointer but text is present

    Perhaps the action event is being generated before a particular variable is being initialized. Have you sprinkled some print statements in your actionPerformed method?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: nullpointer but text is present

    I think you're mistaken about where the error is occurring. Is that the entire stack trace? Which line of the code you posted is line 359 of MenuFontMenu.java?

    BTW, you don't need to compare class names to test if classes are equal. You can use the equality operator:

    Java Code:
    if(sc.getClass() == javax.swing.JComboBox.class) { ... }
    Get in the habit of using standard Java naming conventions!

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,654
    Rep Power
    5

    Default Re: nullpointer but text is present

    Quote Originally Posted by kjkrum View Post
    BTW, you don't need to compare class names to test if classes are equal. You can use the equality operator:

    Java Code:
    if(sc.getClass() == javax.swing.JComboBox.class) { ... }
    Ouch, I would really implore you to just advise people to use the equals() method to compare if two Class instances are the same. Now you have to assume that the OP does not need to be explained that Class object instances are unique within the same classloader, which is why in this particular case it just so happens that using the equality operator magically works while in 99.9% of the cases it won't work for object types and you in fact need to use the equals() method. Its just too much baggage :)

    Or just tell me to take a hike, in which case the next time you do it I will nag you again. And again. And again.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    T do not understand it so,
    I tried something different maybe now the problem is more obvious?


    First I add a series of radio buttons to a JMenu.
    and I add them to a map <Integer,JRadioButtonMenuItem>

    Java Code:
    if (checkboxShort[i].isSelected()) {
                        Font font = new Font(fontname, Font.PLAIN, 16);
                        rb = new JRadioButtonMenuItem();
                        rb.setAction(new AbstractAction() {
                        {
                          putValue(NAME, fontname);
                          putValue(SMALL_ICON, iconImport.getImageIcon("whatever"));
                          putValue(MNEMONIC_KEY, new Integer('N'));
                          putValue(SHORT_DESCRIPTION, "Creates whatever");
                          putValue(LONG_DESCRIPTION, "Creating whatever...");
                        }
                            public void actionPerformed(ActionEvent evt) {
    
                                changeFontSupport.firePropertyChange("FONTCHANGE",
                                                                        false,true);
                                fontMenu2.getCurrentFontInfo().setFontFamily(fontname);
                            }
                        });
    
                        rb.setFont(font);
                        group2.add(rb);
                        shortFontMenu.add(rb);
                        shortFontListMap.put(new Integer(i), rb);

    Some later I get the action form the radio button, which was stored in the map, like this:

    Java Code:
    while (iter.hasNext()) {
                                Map.Entry entry = (Map.Entry)iter.next();
                                Integer indexInteger = (Integer) entry.getKey();
                                rb = (JRadioButtonMenuItem) entry.getValue();
                                comboArray[count] = rb.getAction();
                                count++;
                            }
    Than I add the action to a combo box and render all like this:

    Java Code:
    private class ComboRenderer1 extends BasicComboBoxRenderer {
                
                @Override
                public Component getListCellRendererComponent(JList list,
                        Object value, int index, boolean isSelected,
                                                    boolean cellHasFocus) {
                    super.getListCellRendererComponent(list, value, index,
                                                    isSelected, cellHasFocus);
                    final Object rendervalue = value;
                    Action action = (Action) value;
                    String s = (String) action.getValue(Action.NAME);
                    final String fontFamilyName = s;
                    setFont(new Font(fontFamilyName, Font.PLAIN, 16));
                    this.setText(s);
                    return this;
                }
            }
    Now my question is (after this maybe too wild ride)
    How I get the name of the action out of the action string which looks like this:

    javax.swing.JComboBox[,72,5,122x27,invalid,layout=com.apple.laf.AquaComb oBoxUI$AquaComboBoxLayoutManager,alignmentX=0.0,al ignmentY=0.0,border=,flags=16777536,maximumSize=,m inimumSize=,preferredSize=,isEditable=false,lightW eightPopupEnabled=true,maximumRowCount=8,selectedI temReminder=willemwriter.MenuFontMenu$4@444b0bbb]

    I tried things like this but get nullpointers or casting errors


    Java Code:
    if (sc.getClass().getName().equals("javax.swing.JComboBox"))  { 
                        JComboBox source = (JComboBox) sc;
                        Object o = source.getSelectedItem();
                        Action a = (Action) o;
                        String currentFont = (String) a.getValue(Action.NAME);      
                        System.out.println("item: " + source);
                
                    }

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    Why all this fuzz, you might ask?
    1) I need the fonts to be present at the menu and combo box of the toolbar
    2) I have the selection "centralized" in a single action
    3) I need the references to make the selections appear on both menu and combo box

    not easy for me!

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    In order to get a better understanding of all this I did some testing (like jim said) in printing out things:

    combo1.getSelectedItem()
    willemwriter.MenuFontMenu$4@1dc85444
    willemwriter.MenuFontMenu$4@2a8a1977 // and with another selection

    getActionCommand()
    comboBoxChanged

    getAction()
    null

    combo1.getName()
    null

    I thought I installed an action but I did not, right?

    But before saving the radio button (rb) int the map I did
    rb.setAction(new AbstractAction() {

    and latter: shortFontListMap.put(new Integer(i), rb);
    map of <Integer, JRadioButtonMenuItem>

    So what I am doing here:
    here I get that map: fontMap = fontMenu.getShortFontNameList();

    and put it in a array:
    Java Code:
    while (iter.hasNext()) {
                                Map.Entry entry = (Map.Entry)iter.next();
                                Integer indexInteger = (Integer) entry.getKey();
                                rb = (JRadioButtonMenuItem) entry.getValue();
                                comboArray[count] = rb.getAction();
                                count++;
                            }
    and add the array to the combo box:
    Java Code:
    for (int i =0; i < comboArray.length; i++) {
                                combo1.addItem(comboArray[i]);
                            }
    Last edited by willemjav; 02-07-2014 at 07:45 AM.

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    So here is what should happen at this particular part of the application:
    There are 4 menu items:
    1) All fonts (radio b)
    2) short font list (radio b enabled at first)
    3) setup short list (radio b)
    4) another JMenu

    When selecting 1) the all-font-list appears (where each font appears in its family type)
    When selecting 3) the radio buttons of the all-font-list are change into toggle buttons:
    here I remove the complete JMenu and add a new one.
    When the user adds (or removes) the font selections the 2)-button becomes
    available and when selecting it, I once change again the JMenu at the fourth spot.
    Now I add the short list stored in a map (of index number and radio button).

    So there are three populated JMenus (with all the fonts as radio or toggle and the short list menu)

    Now at my toolbar I want the short list of fonts to appear as a com box as well!

    All this is working fine!

    What is not working:
    -When all the fonts get removed from the short list the combo stays empty which is kind of ugly
    So I need to install the dummy item from the beginning (saying "setup shortlist");
    -I can not get yet the selection of the combo;

    And my biggest issue (f which I should have thought of right away and I did not!) is:
    When a selection is made at eg the All menu list, I want that to be present at the short list
    (if that font is available at the short list) and I want it to appear on the combo!
    And two time visa versa!

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    In order to "remember" the selection I need a reference to all the buttons (menu and toolbar), right?
    But I thought there was a better way....
    Since all should run on actions (and that d-mm combo wont still) why not trying to have
    the selection issue as a property, right?
    So I ran into the class AbstractActionExt, but unfortunately I could not make it work!
    So next I thought all items (menu and combo) have an index (their position at the list).
    Why not storing that index in the action object (the short list index will be absolut to long list).
    With that index I would identify each item and select it (true or false) if I have the reference, wow!

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    It seems to me the site is "under attack"

    willemjav, bodylojohn, gimbal2, gyibjpeyw, gyibkddzz, gyibruybl, gyibzdyef, gyigmyshz, gyijjocjq, gyijvixpg, gyikszplv, gyikzzuzm, gyilxdsbi, gyimzkbzn, gyinjlgfz, gyinuuzpk, gyinyxupx, gyipgasum, gyiqjfnbs, gyirlnbag, gyitodsyc, gyiuhmjkj, gyivusglb, gyixkjjdq, gyixmikgz, gyiyqnpqf, gyizbawwr, gyizmuwvq, JosAH, lfuser, LucaBuzza, PinkRose, SurfMan, tkeairteu, tkebazphs, tkecnwyza, tkedgjyaz, tkeesgzof, tkehbscev, tkehpevrs, tkeiywsfl, tkemchldf, tkeqntwmj, tketbsluv, tkeuoiwxz, tkewvjafx, tkextmoac, tkeyaqydn, tkeywhfxi, tkeyzcdze, tkezdhhnd, yugabibik

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,654
    Rep Power
    5

    Default Re: nullpointer but text is present

    I wish we could fight back!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    Yet another approach. Instead of an Action I added the radio button as an item to the combo.
    I render the button object into a text string:
    final Object rendervalue = value;
    JRadioButtonMenuItem v = (JRadioButtonMenuItem) value;
    String s = (String) v.getActionCommand();
    final String fontFamilyName = s;
    setFont(new Font(fontFamilyName, Font.PLAIN, 16));
    this.setText(s);
    return this;
    Now when selecting a combo item I get this string:


    source: javax.swing.JRadioButtonMenuItem[,0,25,197x20,invalid,alignmentX=0.0,alignmentY=0.0 ,border=com.apple.laf.AquaMenuBorder@6bc5af2e,flag s=264,maximumSize=,minimumSize=,preferredSize=,def aultIcon=,disabledIcon=,disabledSelectedIcon=,marg in=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=false,pressedIcon=,ro lloverEnabled=false,rolloverIcon=,rolloverSelected Icon=,selectedIcon=,text=Adobe Arabic]

    But there is no way I can get the very tail part: text=Adobe Arabic

    man, this is making me dizzy!

  13. #13
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    901
    Rep Power
    2

    Default Re: nullpointer but text is present

    You don't "get" the tail part. What you are looking at is the toString() of the JRadioButtonMenuItem. You should find out why it doens't display the fontname itself. Did you use the constructor that has a text parameter?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    I should say here that, that combo box never registered to that ActionListener!
    It appears anyway because the radio button of the menu is added as a component
    to the combo box, kind of crazy, right?

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    I did both (maybe silly):
    See for String fontname:

    Java Code:
     for (int i = 0; i < allFontNameList.length; i++) {
                final String fontname = checkboxShort[i].getActionCommand();
                // when a font is selected the index is the position at the all font
                // list
                if  (cmd.equals(fontname)) {
                //if the radio button is selected create a new button to add to the
                // list
                    if (checkboxShort[i].isSelected()) {
                        Font font = new Font(fontname, Font.PLAIN, 16);
                        rb = new JRadioButtonMenuItem(fontname);
                        rb.setAction(new AbstractAction() {
                        {
                          putValue(NAME, fontname);
                          putValue(SMALL_ICON, iconImport.getImageIcon("whatever"));
                          putValue(MNEMONIC_KEY, new Integer('N'));
                          putValue(SHORT_DESCRIPTION, "Creates whatever");
                          putValue(LONG_DESCRIPTION, "Creating whatever...");
                        }
                            public void actionPerformed(ActionEvent evt) {
    
                                changeFontSupport.firePropertyChange("FONTCHANGE",
                                                                        false,true);
                                fontMenu2.getCurrentFontInfo().setFontFamily(fontname);
                            }
                        });
    
                        rb.setFont(font);
                        group2.add(rb);
                        shortFontMenu.add(rb);
                        shortFontListMap.put(new Integer(i), rb);

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    901
    Rep Power
    2

    Default Re: nullpointer but text is present

    Quote Originally Posted by willemjav View Post
    I should say here that, that combo box never registered to that ActionListener!
    It appears anyway because the radio button of the menu is added as a component
    to the combo box, kind of crazy, right?
    I appreciate that you need to vent your thoughts online, since talking to someone about it helps in understanding your problem, but you are starting to lose the audience with these random ramblings. I have no idea in which context to place the last comment of yours.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  17. #17
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    This radio b is added to the combo and that is the action string that you see (I believe)

  18. #18
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    I understand what your saying,
    I re-wrote this code already 5 times, it is to complicated...
    I get lost myself!

  19. #19
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    I know that questions like: how to parse an integer into a banana, work just fine at the forum!
    My format is not working, I am seeing that!
    I will improve my java-life, surfman, thanks!

  20. #20
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: nullpointer but text is present

    I think I am able to articulate my question, finally!
    -I added several JRadioButtonMenuItem to a JComboBox
    -I registered an action listener directly to the combo box
    -The event.getActionCommand() of the actionPerformed shows "comboBoxChanged"
    - The event.getSource() shows indeed the toString of a JRadioButtonMenuItem:
    javax.swing.JRadioButtonMenuItem[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,bor der=com.apple.laf.AquaMenuBorder@98c5e67,flags=264 ,maximumSize=,minimumSize=,preferredSize=,defaultI con=,disabledIcon=,disabledSelectedIcon=,margin=ja vax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=false,pressedIcon=,ro lloverEnabled=false,rolloverIcon=,rolloverSelected Icon=,selectedIcon=,text=Abadi MT Condensed Extra Bold]

    My question is: how can I resolve the tail part of the string "text=Abadi MT Condensed Extra Bold"?

    I have tried everything, both nothing worked!

Page 1 of 2 12 LastLast

Similar Threads

  1. Reading a zip/text file present in a MAC Bundle
    By Prasannaa in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2008, 03:05 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
  •