wxDataViewListCtrl null pointer Error

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
johnRipper1
In need of some credit
In need of some credit
Posts: 4
Joined: Sat May 27, 2023 8:29 pm

wxDataViewListCtrl null pointer Error

Post by johnRipper1 »

Hi everyone

i have problem in this code when i call function [showTabbed] in main everything is right but when i take args from user and passed to the second function in the class called addNewItem the error show.

how i can make wxDataViewListCtrl peremented active ?

Image

Code: Select all

#pragma once
#include <wx/wxprec.h>
#include <wx/dataview.h>
#include <wx/wx.h>
#include <format>
#include<chrono>
#include <wx/notebook.h>
#include <iostream>
#include "about.h"
#include "createListener.h"
#include<wx/panel.h>
#include<wx/listctrl.h>
#include "WindowsProject1.h"
class ShowListenerTabbed {
    wxVector<wxVariant> itemData;
    bool check = false;
    wxDataViewListCtrl* listctrl;
public:
    void showTabbed(wxNotebook* window) {
            listctrl = new wxDataViewListCtrl(window, wxID_ANY);
            listctrl->SetSize(1980, 1000);
            listctrl->AppendTextColumn("DDD", wxDATAVIEW_CELL_ACTIVATABLE, 500);
            listctrl->AppendTextColumn("MMM", wxDATAVIEW_CELL_ACTIVATABLE, 500);
            listctrl->AppendTextColumn("YYY", wxDATAVIEW_CELL_ACTIVATABLE, 500);
            listctrl->Show();

            itemData.push_back(wxVariant("Monday"));
            itemData.push_back(wxVariant("June"));
            itemData.push_back(wxVariant("2023"));

            listctrl->AppendItem(itemData);
            itemData.clear();

            wxVariant cellValue;
            listctrl->GetValue(cellValue, 0, 2);
            wxMessageBox("Created");
        

    }
    void addNewItem(std::vector<std::vector<wxString>> collection) {
        for (const auto& element : collection.back()) {
            itemData.push_back(element);
            wxMessageBox(element);
        }
        listctrl->AppendItem(itemData);
        itemData.clear();
        listctrl->Layout();
        wxMessageBox("End");


    }

};
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl null pointer Error

Post by doublemax »

Please show the part where the addNewItem() method gets called. I suspect it's called with a bad pointer to the ShowListenerTabbed instance.
Use the source, Luke!
johnRipper1
In need of some credit
In need of some credit
Posts: 4
Joined: Sat May 27, 2023 8:29 pm

Re: wxDataViewListCtrl null pointer Error

Post by johnRipper1 »

Code: Select all

void CreateListenerDialog::Validators(wxCommandEvent& event) {

    IPv4Validator validation;

    if (validation.is_valid_ipv4_address(hostField->GetValue()) == false) {
        wxMessageBox("Invalid IP",
            "Validation", wxOK | wxICON_ERROR);
    }
     else if (validation.is_valid_port(portField->GetValue()) == false) {
        wxMessageBox("Invalid Port",
            "Validation", wxOK | wxICON_ERROR);
     }
     else if (validation.type_connection_valid(typeConnection->GetStringSelection())== false) {
        wxMessageBox("Please Select Type of Connection",
            "Validation", wxOK | wxICON_ERROR);
    }
    else {
        wxMessageBox("Listener Created Successful",
            "Validation", wxOK | wxICON_INFORMATION);

        listenersCollection.push_back({ hostField->GetValue(),portField->GetValue(),typeConnection->GetStringSelection() });
        if (remover == true) { // remove the first section of listenersCollection 
            listenersCollection.erase(listenersCollection.begin());
            remover = false;
        }
        ShowListenerTabbed tab;
        tab.addNewItem(listenersCollection);
     }
};
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl null pointer Error

Post by doublemax »

Code: Select all

        ShowListenerTabbed tab;
        tab.addNewItem(listenersCollection);
This makes no sense at all.

If the code you posted in your first post is complete, there is no custom default constructor for ShowListenerTabbed. So all its members will be 0 or uninitialized, and therefore calling any of its method will crash.

On top of that you're creating the object on the stack, which means that it gets destroyed immediately when it goes out of scope.

What you probably want to do:
In the CreateListenerDialog constructor, create an instance of ShowListenerTabbed with new and store its pointer in a member variable. Then you can access it in the event handler CreateListenerDialog::Validators()
Use the source, Luke!
Post Reply