Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4055742
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:44:22+00:00 2026-05-20T14:44:22+00:00

I have created a JFrame with a canvas in it, like you can see

  • 0

I have created a JFrame with a canvas in it, like you can see in the code below. What I am trying to do is to make the openGL context resize when the size of the screen gets updated. This should be as easy as a call to glViewport(), which I am doing in the event handler called resize(). That, however, results in this:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.lwjgl.opengl.GL11.glViewport(GL11.java:3199)
at gltest.GameMain.resize(GameMain.java:149)
at gltest.GameMain$1.componentResized(GameMain.java:59)
(...)

I am completely clueless. The boundaries returned from the size of the canvas seem to be allright, but once I use them on the viewport, they throw errors. The same happens when I instead make the viewport call like “glViewport(1, 1, 100, 100)”. All these values are well within the boundaries of the window, yet still it throws the very same nullPointerExceptions when I resize the window.

I am out of ideas and energy to figure out why (I have been googling for 3 hours now, no result). What am I doing wrong?

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.util.glu.GLU.*;
import static org.lwjgl.opengl.GL11.*;

public class GameMain {
    public JFrame frame;
    public Canvas canvas;

    public boolean initialize(int width, int height) {
        try {
            Canvas canvas = new Canvas();
            JFrame frame = new JFrame("Open Rock Raiders - Delta");
            this.canvas = canvas;
            this.frame = frame;
            ComponentAdapter adapter = new ComponentAdapter() {
                public void componentResized(ComponentEvent e) {
                    resize();
                }
            };

            canvas.addComponentListener(adapter);
            canvas.setIgnoreRepaint(true);
            frame.setSize(640, 480);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(canvas);
            frame.setVisible(true);
            Dimension dim = this.canvas.getSize();

            Display.setLocation(100, 100);
            Display.setTitle("GL Window");
            Display.setDisplayMode(new DisplayMode(dim.width, dim.height));
            Display.setParent(canvas);
            Display.create();

            //openGL setup
            glViewport(0, 0, dim.width, dim.height);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluPerspective(60.0f, (float)(dim.width/dim.height), 0.1f, 10000.0f);
            glMatrixMode(GL_MODELVIEW);
            glClearColor(94.0f/255.0f, 161.0f/255.0f, 255.0f/255.0f, 0.5f);
            glClearDepth(1.0);
            glShadeModel(GL_FLAT);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LEQUAL);
            glEnable(GL11.GL_CULL_FACE); 
            glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        } catch (LWJGLException le) {
            le.printStackTrace();
        }
        return false;
    }

    public void resize()
    {
        Dimension dim = this.canvas.getSize();
        GL11.glViewport(0, 0, dim.width, dim.height);
    }
}
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:44:23+00:00Added an answer on May 20, 2026 at 2:44 pm

    You should do all of the display operations from the main thread. If you look at the example you posted in your other question, you can see the new canvas size is stored and then updated by the main thread. It’s done thread-safely too. You need to do that and then have a loop after your graphics initialisation something like this:

    while (!closeRequested) {
        GL11.glViewport(0, 0, dim.width, dim.height);
        Display.update();
    }
    
    //finished
    Display.destroy();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have created some demo code, see below. What I am seeing is
I have created jframe in which jpanels are added dynamically what i can't do
I have created a JFrame, and attempting to get it's size is giving an
I have created a JFrame and I would like to associate a specific behaviour
I have created 2 classes as follows... public class A extends JFrame { public
I created a window using the design panel in JFrame, it didn't have any
I have created a JFrame and want to hide this from the taskbar in
I have created a little example(below) on my attempt to safely stop multiple threads
I have created a JFrame form using netbeans GUI builder and place buttons on
I have created a simple JFrame with two labels, two fields and two buttons.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.