简体   繁体   中英

JTable chokes on custom renderer

I have a custom renderer in a JTable . When my JTable displays, I get a NullPointerException on JTable.prepareRenderer() . I'm not sure what part of my custom renderer isn't preparable, but I'm sure it's my fault.

Condensed version of my renderer (full version at this spot ):

public class GradeRenderer extends JLabel implements TableCellRenderer {    
    public Component getTableCellRendererComponent(JTable table, Object grade,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (grade == null) return new JLabel();
        Grade myself = (Grade) grade;
        int score = myself.getScore();
        setText(String.valueOf(score));
        Assessment thing = myself.getThing();
        GradeStatus status = myself.getStatus();
        AssessmentType type = thing.getType();
        // do some work with status
        // do some work with type
        return this;
    }
}

Code where I add the renderer (full version here ):

model = new GradeEntryModel();
table = new JTable(model);
// some work with table
table.setDefaultRenderer(Grade.class, new GradeRenderer(true));

Any pointers as to where I've gone wrong?

The NPE message will give you the statement causing the error. So that is the place to start looking. Once you know which variable is null, you can start to solve the problem. We can't help you because we have no idea where the error is occuring.

The only think I can suggest is that you should not use "return null" to return a null renderer. You would set the text to "" or something like that and then return the renderer.

If you need more help then post your SSCCE that demonstrates the problem.

I agree with camickr, the NPE is telling you what is null so that should give you a clue.

But looking at your code i guess

 if (grade == null) return null;

returning null is a good way to trigger a NullPointerException

Turns out some of my data in model is null . That'd do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM