1

The title roughly sums it up. Why can I not do this?

private void render() {
    bs = window.getCanvas().getBufferStrategy();

    if (bs == null) {
        bs = window.getCanvas().createBufferStrategy(3); // this is the line with the error: cannot convert from void to BufferStrategy.
    }
}

however I can do createBufferStrategy(3) with no variable. Why is this?

GoldShovel
  • 35
  • 7

1 Answers1

1

createBufferStrategy returns void (or, well, doesn't return anything). It does not the return BufferStrategy it creates.

Matt
  • 43,482
  • 6
  • 101
  • 102
  • ah okay. So now I've done this instead: if (window.getCanvas().getBufferStrategy() == null) { window.getCanvas().createBufferStrategy(3); return; } – GoldShovel Jul 14 '18 at 13:34
  • however this is returning an NPE, any idea why? – GoldShovel Jul 14 '18 at 13:34
  • @GoldShovel I assume `getCanvas()` must be returning `null` there – Matt Jul 14 '18 at 14:49
  • nearly.. canvas was actually null, because I never set it to an instance of my JFrame init class.. My final problem, after creating the bufferstrategy with canvas.getCanvas().createBufferStrategy(3) my check still says that bs is null.. Any clue why. – GoldShovel Jul 14 '18 at 15:07
  • @GoldShovel are you re-assigning `bs = ...` after creating? E.g. `if (bs == null) { whatever.createBufferStrategy(3); bs = whatever.getBufferStrategy(); }` – Matt Jul 14 '18 at 15:18
  • yeah realised I should've put 2 pieces of code the other way round... final problem :/ bs.show() doesn't seem to be displaying anything to my JFrame. g (Graphics variable) seems to be set with the right data. Just no idea what's wrong. The "error" is in the method init() Code is here: https://hastebin.com/aduhepohop.java i really do appreciate your help. – GoldShovel Jul 14 '18 at 15:28
  • @GoldShovel I'm not familiar with BufferStrategy itself anymore (I used it 15 years ago..). You might have to peruse other answers for clues. Try starting here: https://stackoverflow.com/a/13590198 – Matt Jul 14 '18 at 15:37