3

I have a problem with making the mainwindow on my simple app run, The error given is that - Object reference not set to an instance of an object.

this happens when the app is getting debugged and the error occurs at handler.window1.ShowAll()

I did find some code online which hints at adding some member code as in member this.Whatever() = window1 however i have no idea if this is relevent to my code, or where to put it.

i am happy for any help you can give me as i have been trying all day to get this working in many ways and simply cannot.

namespace potato
module Main =
 open System
 open Gtk
 type Handler()=class
 [<Object>]
 [<DefaultValue(true)>]
 val mutable window1 : Window
 end
 [<EntryPoint>]
 let Main(args) = 
 Application.Init()
 let builder = new Builder("GUI.ui")
 let handler = new Handler()
 builder.Autoconnect(handler)
 handler.window1.ShowAll()
 Application.Run()
 0

Here is the glade.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
 <requires lib="gtk+" version="3.18"/>
 <object class="GtkWindow" id="window1">
 <property name="width_request">1024</property>
 <property name="height_request">576</property>
 <property name="can_focus">False</property>
 <child>
 <placeholder/>
 </child>
 </object>
</interface>
asked Mar 1, 2017 at 18:56
6
  • 2
    Who and where creates an instance of Window and assigns it to window1? As the error is saying, it's null at the time ShowAll is being called. Commented Mar 1, 2017 at 20:24
  • yep, i do realise this, i was struggling code this properly as i am still very much a learner, i have managed to get some help to solve it and understand it a bit better, i will check and if so will post the result Commented Mar 1, 2017 at 20:33
  • Nothing wrong with not knowing things, no reason to be apologetic. Just meant to ask for clarification/drop you a hint. Commented Mar 1, 2017 at 21:02
  • its still eluding me at the moment, but will give it another go tomorow. Commented Mar 1, 2017 at 22:47
  • @scrwtp, i can't figure this out, as far as i can tell the window1 is not being passed along, and i cant figure how to do this Commented Mar 2, 2017 at 13:25

1 Answer 1

3

Right, the problem was right in front of my eyes, and i ended up having to go through old test projects to see and realise what @scrwtp was hinting at, this is the old working code fixed for gtk3 gtkbuilder.

namespace potato
module Main =
open System
open Gtk
type Handler()=class
 [<Builder.Object>]
 [<DefaultValue(true)>]
 val mutable window1 : Window
end
let OnDelete (args:DeleteEventArgs) =
 Application.Quit()
 args.RetVal <- true 
[<EntryPoint>]
let Main (args) = 
 Application.Init()
 let gxml = new Builder("GUI.xml")
 let handler = new Handler()
 do gxml.Autoconnect(handler)
 handler.window1.DeleteEvent
 |> Event.add OnDelete
 handler.window1.ShowAll()
 Application.Run()
 0

The reason, i now understand is that i had specified a handler and passed nothing to it, because nothing was passed it IE:(handler.window1.DeleteEvent) it simply wouldnt show when i called showall, hope this helps someone else with a similar problem

answered Mar 2, 2017 at 15:24
6
  • For my own understanding - the actual missing bit was the gxml.Autoconnect(handler) line, right? I suppose that instantiates the object graph described by the xml and connects it to the handler object, so that the window1 property on the handler gets set to the GtkWindow object from the xml? Commented Mar 2, 2017 at 19:52
  • no, as far as i can tell the autoconnect was connected originally just as builder rather than gxml, but as i hadn't added a window1 event, there was nothing to do, therefore no window and null, i think it could have been any event at all just to specify, that it had something to do Commented Mar 2, 2017 at 21:13
  • at least i hope that was it, just a blank handler ? Commented Mar 2, 2017 at 21:25
  • I could imagine this being the case, but there's nothing about the delete event in the line where you saw an error (the ShowAll call). What would happen if you now dropped the Autoconnect line? Disclaimer: I know nothing about gtk Commented Mar 2, 2017 at 21:33
  • I will give this try, i am pretty sure that without the Autoconnect though it would end up having to be recoded to be a simple Gtk app minus the external glade interface designer Commented Mar 3, 2017 at 10:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.